【问题标题】:Laravel 5.3 - Auth Scaffolding How Are Errors InsertedLaravel 5.3 - Auth Scaffolding 如何插入错误
【发布时间】:2017-01-31 19:06:43
【问题描述】:

我对 Laravel 比较陌生,并试图理解一些东西。我创建了一个基本项目并使用了`

` php artisan make:auth

` 生成身份验证脚手架。

在生成的视图中,$errors 变量可用。我知道这可以通过使用 withErrors() 方法插入到视图中。

但是,我似乎无法在示例中找到它是如何插入的。在后台,以下函数似乎正在处理注册:

    /**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return $this->registered($request, $user)
                    ?: redirect($this->redirectPath());
}

所以调用了默认RegisterController的validator方法,它返回了一个validator。但我无法理解验证器的错误是如何插入到 auth.register 视图中的。

【问题讨论】:

    标签: php laravel laravel-5.3 laravel-facade illuminate-container


    【解决方案1】:

    当验证错误发生时,Laravel 会抛出异常。在这种情况下,会抛出 ValidationException 的实例。

    Laravel 使用 Illuminate\Foundation\Exceptions\Handler 类处理任何未捕获的异常。在您的应用程序中,您应该看到一个在 app/Exceptions/Handler.php 中扩展它的类。在该类中,您将看到 render 方法调用其父级的 render 方法,如果您检查代码包含以下行:

    public function render($request, Exception $e)
    {
        $e = $this->prepareException($e);
    
        if ($e instanceof HttpResponseException) {
            return $e->getResponse();
        } elseif ($e instanceof AuthenticationException) {
            return $this->unauthenticated($request, $e);
        } elseif ($e instanceof ValidationException) {
            return $this->convertValidationExceptionToResponse($e, $request);
        }
    
        return $this->prepareResponse($request, $e);
    }
    

    如果你在同一个类中进一步检查,在方法@​​987654327@ 中,你可以看到 Laravel 将错误闪现到响应并重定向回来,输入(当请求不期望 JSON 时):

    protected function convertValidationExceptionToResponse(ValidationException $e, $request)
    {
        if ($e->response) {
            return $e->response;
        }
    
        $errors = $e->validator->errors()->getMessages();
    
        if ($request->expectsJson()) {
            return response()->json($errors, 422);
        }
    
        return redirect()->back()->withInput($request->input())->withErrors($errors);
    }
    

    【讨论】:

      【解决方案2】:

      RegisterController 扩展了 Controller,如果我们查看类 Controller 我们可以看到 use trait Illuminate\Foundation\Validation\ValidatesRequests;

      如果我们深入研究,我们会发现:

      /**
           * Create the response for when a request fails validation.
           *
           * @param  \Illuminate\Http\Request  $request
           * @param  array  $errors
           * @return \Symfony\Component\HttpFoundation\Response
           */
          protected function buildFailedValidationResponse(Request $request, array $errors)
          {
              if ($request->expectsJson()) {
                  return new JsonResponse($errors, 422);
              }
      
              return redirect()->to($this->getRedirectUrl())
                              ->withInput($request->input())
                              ->withErrors($errors, $this->errorBag());
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-28
        • 1970-01-01
        • 2017-03-14
        • 2017-01-04
        • 2017-03-30
        • 2017-04-30
        • 2017-01-22
        • 1970-01-01
        相关资源
        最近更新 更多