mayoz/lumen-form-request 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mayoz/lumen-form-request

Composer 安装命令:

composer require mayoz/lumen-form-request

包简介

Adopt the Laravel Form Request to Lumen framework.

README 文档

README

Latest Version on Packagist Software License StyleCI

Adopt the Laravel Form Request to Lumen framework.

Installation

You can install the package via composer:

composer require mayoz/lumen-form-request

Register the service provider in your boostrap/app.php configuration file:

$app->register(Illuminate\Foundation\Providers\FormRequestServiceProvider::class);

Usage

Let's continue the official Laravel's documantation.

Format Validation

If you want to format the verification messages, follow these steps:

Firstly import the ValidationException class:

use Illuminate\Validation\ValidationException;

Add (if need) the dontReport list:

/**
 * A list of the exception types that should not be reported.
 *
 * @var array
 */
protected $dontReport = [
    // ...
    ValidationException::class,
];

And finally update the render method:

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Throwable  $exception
 * @return \Symfony\Component\HttpFoundation\Response
 *
 * @throws \Throwable
 */
public function render($request, Throwable $exception)
{
    if ($exception instanceof ValidationException) {
        return $this->convertValidationExceptionToResponse($exception, $request);
    }

    return parent::render($request, $exception);
}

/**
 * Create a response object from the given validation exception.
 *
 * @param  \Illuminate\Validation\ValidationException  $e
 * @param  \Illuminate\Http\Request  $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
{
    if ($e->response) {
        return $e->response;
    }

    return response()->json([
        'message' => $e->getMessage(),
        'errors' => $e->errors(),
    ], $e->status);
}

License

This package is licensed under The MIT License (MIT).

统计信息

  • 总下载量: 2.52k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固