回答
在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