回答
在Laravel 5中,您可以通过编辑app/Exceptions/Handler.php
中的render
方法来捕捉异常。
如果您想查找缺少的页面(也称为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
是Symfony\Component\HttpKernel\Exception\NotFoundHttpException
一个instanceof
如果是我们发response
与view文件error.404
作为与HTTP status code 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);
}
由于提交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仍处于重发展,可能会再次发生变化。
我希望不会马上改变! – 2015-02-18 07:06:01
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);
}
也一样,但忘了'$ this-> isHttpException($ e)'。谢谢 – midan888 2015-03-04 08:16:47
您可以处理它的一种方法,但它不是最好的方法是切换到重定向。
<?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);
}
}
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);
}
将正常工作对我来说
如果你想保持处理器在您的网页routes文件,您现有的路线后:
Route::any('{all}', function ($missingRoute) {
// $missingRoute
})->where('all', '(.*)');
- 1. 如何捕捉SQL异常在Laravel 5
- 2. 如何捕获Laravel 5中的包生成的异常?
- 3. 如何在异常中捕获异常?
- 4. 捕获异常失败
- 5. Laravel 5缺少页面和authed用户
- 6. 如何在Laravel 4.2中捕获PDO异常?
- 7. 捕获异常aspx.vb页
- 8. LaravelCollective SSH尝试捕获异常失败
- 9. 无法捕获异常页面上的异常
- 10. Laravel 5中的API异常
- 11. 其余路径变量缺失时捕获异常API
- 12. 如何在FutureTask中捕获异常
- 13. 在GNU Octave中,如何捕获异常
- 14. 如何在iOS中捕获异常?
- 15. 如何在PowerShell中捕获异常?
- 16. 如何在Controller中捕获java.lang.NumberFormatException.forInputString异常?
- 17. 如何在实体中捕获异常?
- 18. 如何捕获页面抛出一个异常
- 19. 如何捕获Java中缺少对象引起的异常?
- 20. 未捕获错误:INVALID_CHARACTER_ERR:DOM异常5
- 21. 由于未捕获的异常'NSRangeException'xcode 5
- 22. 如何在Laravel 5中获取当前页面的细分?
- 23. 如何在laravel 5中获得自定义错误页面?
- 24. Laravel 5 - 如何捕获Mail :: send()错误?
- 25. 如何捕获此异常
- 26. 如何捕获Interop异常?
- 27. 如何捕获substr异常?
- 28. 如何捕获entitydatasource异常
- 29. 如何捕获ReactiveCommand异常?
- 30. 如何捕获jquery异常?
你怎么知道这个答案,我在哪里可以找到关于此的更多信息? – 2015-02-18 07:00:21
@adityamenon,它是Laravel 5以前的开发版本。我已经重写它以匹配当前版本的Laravel 5. – Marwelln 2015-02-18 10:29:04
Aditya Menon,Handler.php的基础知识和render()函数在文档http://laravel.com/docs/5.0/errors(但Marwelln的回答是如何使用它的一个很好的例子)。 – orrd 2015-02-27 05:56:11