在 Laravel 5 中,App::missing
并且App::error
不可用,那么您现在如何捕获异常和丢失的页面?
我在文档中找不到有关此的任何信息。
在 Laravel 5 中,您可以通过编辑render
.app/Exceptions/Handler.php
如果要捕获丢失的页面(也称为NotFoundException
),则需要检查异常 ,$e
是否是Symfony\Component\HttpKernel\Exception\NotFoundHttpException
.
public function render($request, Exception $e) {
if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
return response(view('error.404'), 404);
return parent::render($request, $e);
}
使用上面的代码,我们检查是否$e
是一个instanceof
of Symfony\Component\HttpKernel\Exception\NotFoundHttpException
,如果是,我们发送一个response
带有视图文件error.404
的内容作为HTTP 状态代码 404。
这可以用于任何异常。因此,如果您的应用程序发出 的异常App\Exceptions\MyOwnException
,则改为检查该实例。
public function render($request, Exception $e) {
if ($e instanceof \App\Exceptions\MyOwnException)
return ''; // Do something if this exception is thrown here.
if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
return response(view('error.404'), 404);
return parent::render($request, $e);
}
从今天开始提交(9db97c3),您需要做的就是在 /resources/views/errors/ 文件夹中添加一个 404.blade.php 文件,它会自动找到并显示它。
自提交30681dc和9acf685以来,该missing()
方法已移至Illuminate\Exception\Handler
该类。
所以,有一段时间,你可以这样做:
app('exception')->missing(function($exception)
{
return Response::view('errors.missing', [], 404);
});
...但是最近,在62ae860中进行了重构。
现在,您可以将以下内容添加到app/Http/Kernel.php
:
// add this...
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Response;
class Kernel extends HttpKernel {
(...)
public function handle($request)
{
try
{
return parent::handle($request);
}
// ... and this:
catch (NotFoundHttpException $e)
{
return Response::view('errors.missing', [], 404);
}
catch (Exception $e)
{
throw $e;
}
}
最后,请记住 Laravel 仍在大力开发中,可能会再次发生变化。
如果您想在现有路由之后将处理程序保留在您的网络路由文件中:
Route::any( '{all}', function ( $missingRoute) {
// $missingRoute
} )->where('all', '(.*)');
Angular HTML5 模式可能会导致大量页面丢失(用户为少数页面添加书签并尝试重新加载)。如果您无法覆盖服务器设置来处理该问题,您可能会考虑覆盖丢失的页面行为。
您可以修改 \App\Exceptions\Handler 渲染方法以使用 200 推送内容,而不是使用 404 代码推送 404 模板。
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($this->isHttpException($e) && $e->getStatusCode() == 404)
{
return response()->view('angular', []);
}
return parent::render($request, $e);
}
Laravel 附带默认错误页面,您可以像这样轻松添加自定义错误页面
1 - 在视图 -> 错误文件夹中创建错误视图
2 - 名称必须匹配 HTTP 错误,例如 404 或 403 500
`Route::get('/page/not/found',function($closure){
// second param is optional
abort(404, 'Page Not found');
});`
通过添加以下代码
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
];
和
public function render($request, Exception $e)
{
return redirect('/');
//return parent::render($request, $e);
}
会为我正常工作
您可以处理它但不是最好的一种方法是切换到重定向。
<?php namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler {
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
return redirect('/');
//return parent::render($request, $e);
}
}