「Php/laravel/laravel5/exception」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→その他パターンはこちらに書いてある) |
(→404や403や500をカスタマイズする場合) |
||
行6: | 行6: | ||
if ($exception->getStatusCode() == 404) { | if ($exception->getStatusCode() == 404) { | ||
return response()->make("404"); | return response()->make("404"); | ||
− | + | } else { | |
− | + | return response()->make("500"); | |
− | + | ||
} | } | ||
return parent::render($request, $exception); | return parent::render($request, $exception); |
2018年2月16日 (金) 11:14時点における最新版
404や403や500をカスタマイズする場合
app/Exceptions/Handler.php
public function render($request, Exception $exception) { if ($this->isHttpException($exception)) { if ($exception->getStatusCode() == 404) { return response()->make("404"); } else { return response()->make("500"); } return parent::render($request, $exception); }
jsonにする場合
app/Exceptions/Handler.php
return response()->json([ 'status' => 500, 'errors' => $this->getMessage(500) ], 500);
viewに渡す場合
app/Exceptions/Handler.php
if($e->getStatusCode() == 403) { return response()->view('errors.403'); }
responseのメソッドはこちらに書いてある
vendor/laravel/framework/src/Illuminate/Contracts/Routing/ResponseFactory.php
make($content = , $status = 200, array $headers = []) view($view, $data = [], $status = 200, array $headers = []) json($data = [], $status = 200, array $headers = [], $options = 0) jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0) stream($callback, $status = 200, array $headers = []) download($file, $name = null, array $headers = [], $disposition = 'attachment') redirectTo($path, $status = 302, $headers = [], $secure = null) redirectToRoute($route, $parameters = [], $status = 302, $headers = []) redirectToAction($action, $parameters = [], $status = 302, $headers = []) redirectGuest($path, $status = 302, $headers = [], $secure = null) redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null)