自定義HTTP錯(cuò)誤頁(yè)面
描述:自定義錯(cuò)誤頁(yè)面,例如404頁(yè)面不存在堂竟、500服務(wù)器錯(cuò)誤...
解決:
Larave5中自定義錯(cuò)誤與異常的處理位于app/Exceptions/Handler.php
/**
* 在HTTP響應(yīng)中回執(zhí)異常頁(yè)面
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if($this->isHttpException($exception)){
//對(duì)于HTTP異常,如404席楚、503等税稼,調(diào)用的是renderHttpException()
return $this->renderHttpException($exception);
}else{
return parent::render($request, $exception);
}
}
renderHttpException()
方法位于\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php
文件中
protected function renderHttpException(HttpException $e)
{
$status = $e->getStatusCode();
$paths = collect(config('view.paths'));
view()->replaceNamespace('errors', $paths->map(function ($path) {
return "{$path}/errors";
})->push(__DIR__.'/views')->all());
if (view()->exists($view = "errors::{$status}")) {
return response()->view($view, ['exception' => $e], $status, $e->getHeaders());
}
return $this->convertExceptionToResponse($e);
}
根據(jù)代碼邏輯郎仆,需在\resources\views\
目錄下創(chuàng)建errors
目錄,存放以錯(cuò)誤狀態(tài)碼命名的blade
模板抛寝。
自定義HTTP錯(cuò)誤頁(yè)面