多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
1.创建验证文件。 ``` php artisan module:make-request LoginRequest AuthAdmin ``` 2.编写验证文件。 ``` <?php namespace Modules\AuthAdmin\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class LoginRequest extends FormRequest { /** * php artisan module:make-request LoginRequest AuthAdmin */ /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'username' => 'required|regex:/^[a-zA-Z0-9]{4,14}$/', 'password' => 'required|regex:/^[a-zA-Z0-9]{4,14}$/' ]; } public function messages(){ return [ 'username.required' => '请输入用户名!', 'username.regex' => '用户名必须4到14位的数字或字母!', 'password.required' => '请输入密码!', 'password.regex' => '密码必须4到14位的数字或字母!', ]; } } ``` 3.引用验证文件。 ``` <?php namespace Modules\AuthAdmin\Http\Controllers\v1; use Illuminate\Http\Request; use Modules\AuthAdmin\Http\Requests\LoginRequest; use Modules\AuthAdmin\Models\AuthAdmin as AuthAdminModel; class LoginController extends CommonController { /** * @name 用户登录 * @method POST * @param username 账号 * @param password 密码 */ public function login(LoginRequest $request) { return $request->all(); return (new AuthAdminModel())->login($request->all()); } } ``` 4。修改app\Exceptions\Handler.php。 ``` <?php namespace App\Exceptions; use Exception; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Validation\ValidationException; class Handler extends ExceptionHandler { /** * A list of the exception types that are not reported. * * @var array */ protected $dontReport = [ // ]; /** * A list of the inputs that are never flashed for validation exceptions. * * @var array */ protected $dontFlash = [ 'password', 'password_confirmation', ]; /** * Report or log an exception. * * @param \Exception $exception * @return void * * @throws \Exception */ public function report(Exception $exception) { parent::report($exception); } /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Symfony\Component\HttpFoundation\Response * * @throws \Exception */ public function render($request, Exception $exception) { if($request->is("api/*")){ //如果错误是 ValidationException的一个实例,说明是一个验证的错误 if($exception instanceof ValidationException){ $result = [ "code"=>4222, "msg"=>array_values($exception->errors())[0][0], ]; return response()->json($result,400); } } return parent::render($request, $exception); } } ``` 视频链接 ```[youku] XNDU0MDUxNDgyMA ```