如何在 Laravel 5 中捕获异常/丢失的页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26630985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I catch exceptions / missing pages in Laravel 5?
提问by Marwelln
In Laravel 5, App::missing
and App::error
is not available, so how do your catch exceptions and missing pages now?
在 Laravel 5 中,App::missing
并且App::error
不可用,那么您现在如何捕获异常和丢失页面?
I could not find any information regarding this in the documentation.
我在文档中找不到任何关于此的信息。
回答by Marwelln
In Laravel 5 you can catch exceptions by editing the render
method in app/Exceptions/Handler.php
.
在Laravel 5中,您可以通过编辑捕获异常render
的方法app/Exceptions/Handler.php
。
If you want to catch a missing page (also known as NotFoundException
) you would want to check if the exception, $e
, is an instance of Symfony\Component\HttpKernel\Exception\NotFoundHttpException
.
如果您想捕获丢失的页面(也称为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);
}
With the code above, we check if $e
is an instanceof
of Symfony\Component\HttpKernel\Exception\NotFoundHttpException
and if it is we send a response
with the viewfile error.404
as content with the HTTP status code 404.
使用上面的代码,我们检查是否$e
是一个instanceof
,Symfony\Component\HttpKernel\Exception\NotFoundHttpException
如果是,我们发送一个response
带有视图文件error.404
作为内容的HTTP 状态代码 404。
This can be used to ANY exception.So if your app is sending out an exception of App\Exceptions\MyOwnException
, you check for that instance instead.
这可以用于任何异常。因此,如果您的应用发出 异常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);
}
回答by Cooper Sellers
With commit from today (9db97c3), all you need to do is add a 404.blade.php file in the /resources/views/errors/ folder and it will find and display it automatically.
从今天开始提交(9db97c3),您需要做的就是在 /resources/views/errors/ 文件夹中添加一个 404.blade.php 文件,它会自动查找并显示它。
回答by Gras Double
Since commits 30681dcand 9acf685the missing()
method had been moved to the Illuminate\Exception\Handler
class.
自提交30681dc和9acf685以来,该missing()
方法已移至Illuminate\Exception\Handler
类中。
So, for some time, you could do this:
所以,有一段时间,你可以这样做:
app('exception')->missing(function($exception)
{
return Response::view('errors.missing', [], 404);
});
... But then recently, a refactoring has been done in 62ae860.
... 但是最近,在62ae860 中进行了重构。
Now, what you can do is adding the following to app/Http/Kernel.php
:
现在,您可以做的是将以下内容添加到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;
}
}
Finally, please keep in mind Laravel is still under heavy development, and changes may occur again.
最后,请记住 Laravel 仍在大力开发中,可能会再次发生变化。
回答by Sándor Tóth
Angular HTML5 mode could cause a bunch of missing pages (user bookmark few page and try to reload). If you can't override server settings to handle that, you might thinking of override missing page behaviour.
Angular HTML5 模式可能会导致一堆丢失的页面(用户为几页添加书签并尝试重新加载)。如果您无法覆盖服务器设置来处理该问题,您可能会考虑覆盖丢失的页面行为。
You can modify \App\Exceptions\Handler render method to push content with 200 rather than push 404 template with 404 code.
您可以修改 \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);
}
回答by S..
In case you want to keep the handler in your web routes file, after your existing routes:
如果您想在您的网络路由文件中保留处理程序,请在现有路由之后:
Route::any( '{all}', function ( $missingRoute) {
// $missingRoute
} )->where('all', '(.*)');
回答by mdamia
Laravel ships with default error page, You can easily add custom error pages like this
Laravel 附带默认错误页面,您可以轻松添加这样的自定义错误页面
1 - Create an error view in view -> errors folder
2 - The name must match HTTP errors like 404 or 403 500
1 - 在视图中创建错误视图 -> 错误文件夹
2 - 名称必须匹配 HTTP 错误,如 404 或 403 500
`Route::get('/page/not/found',function($closure){
// second param is optional
abort(404, 'Page Not found');
});`
回答by Swapnil Bijwe
By Adding following code
通过添加以下代码
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
];
and
和
public function render($request, Exception $e)
{
return redirect('/');
//return parent::render($request, $e);
}
will work properly for me
会为我正常工作
回答by segFault42
One way you can handle it but it's not the best is to change over to a redirect.
您可以处理它但不是最好的一种方法是更改为重定向。
<?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);
}
}