未定义管线时,Laravel如何创建404视图
我一直在努力理解《风帽下的拉威尔》,因为就我个人而言,这有助于我学习和记住某些东西是如何工作的。我无法计算出基本的404是如何工作的 我在157上的未定义管线时,Laravel如何创建404视图,laravel,laravel-5,laravel-blade,Laravel,Laravel 5,Laravel Blade,我一直在努力理解《风帽下的拉威尔》,因为就我个人而言,这有助于我学习和记住某些东西是如何工作的。我无法计算出基本的404是如何工作的 我在157上的illumb\RoutingRouteCollection.php中尝试了很多var_转储 public function match(Request $request) { $routes = $this->get($request->getMethod()); // First, we will see if we
illumb\RoutingRouteCollection.php
中尝试了很多var_转储
public function match(Request $request)
{
$routes = $this->get($request->getMethod());
// First, we will see if we can find a matching route for this current request
// method. If we can, great, we can just return it so that it can be called
// by the consumer. Otherwise we will check for routes with another verb.
$route = $this->matchAgainstRoutes($routes, $request);
if (! is_null($route)) {
return $route->bind($request);
}
// If no route was found we will now check if a matching route is specified by
// another HTTP verb. If it is we will need to throw a MethodNotAllowed and
// inform the user agent of which HTTP verb it should use for this route.
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0) {
return $this->getRouteForMethods($request, $others);
}
throw new NotFoundHttpException;
}
看起来好像输出404视图的一定是NotFoundHttpException,但我不知道如何输出?Laravel提供了一个默认的
App\Exceptions\Handler
,它实现了一个render
方法,该方法就是如何将异常转换为HTTP响应的
异常处理程序由try-catch块中的
Http/Kernel.php
调用 当找不到页面时,它将抛出NotFoundHttpException
有中止方法vendor/laravel/framework/src/light/Foundation
public function abort($code, $message = '', array $headers = [])
{
if ($code == 404) {
throw new NotFoundHttpException($message);
}
throw new HttpException($code, $message, null, $headers);
}
laravel为错误代码提供了不同的视图:内部异常/视图
有可用于错误代码的视图
401,403,404,419,429,500,503
现在,在illumb\Foundation\Exceptions
内部Handler.php
:renderHttpException方法用于根据异常的状态代码呈现视图
比如:
1) renderHttpException:此方法检查视图是否存在给定的状态代码,然后返回视图
/**
* Render the given HttpException.
*
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function renderHttpException(HttpException $e)
{
$this->registerErrorViewPaths();
if (view()->exists($view = "errors::{$e->getStatusCode()}")) {
return response()->view($view, [
'errors' => new ViewErrorBag,
'exception' => $e,
], $e->getStatusCode(), $e->getHeaders());
}
return $this->convertExceptionToResponse($e);
}
2) RegisterErrorViewPath:此方法将为错误视图注册路径
/**
* Register the error template hint paths.
*
* @return void
*/
protected function registerErrorViewPaths()
{
$paths = collect(config('view.paths'));
View::replaceNamespace('errors', $paths->map(function ($path) {
return "{$path}/errors";
})->push(__DIR__.'/views')->all());
}
现在,如果要创建自定义404页面并呈现该页面,请执行以下操作:
内部app/Exceptions/Handler.php
public function render($request, Exception $exception)
{
if($this->isHttpException($exception)){
switch ($exception->getStatusCode()) {
case 404:
return view('path-to-custom404-here');
break;
}
}
return parent::render($request, $exception);
}