「Php/laravel/laravel5/exception」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==404や403や500をカスタマイズする場合== app/Exceptions/Handler.php public function render($request, Exception $exception) { if ($this->isHttpExcept...」) |
(→404や403や500をカスタマイズする場合) |
||
(同じ利用者による、間の1版が非表示) | |||
行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); | ||
行26: | 行25: | ||
} | } | ||
− | == | + | ==responseのメソッドはこちらに書いてある== |
vendor/laravel/framework/src/Illuminate/Contracts/Routing/ResponseFactory.php | vendor/laravel/framework/src/Illuminate/Contracts/Routing/ResponseFactory.php | ||
− | make( | + | make($content = '', $status = 200, array $headers = []) |
− | view( | + | view($view, $data = [], $status = 200, array $headers = []) |
− | json( | + | json($data = [], $status = 200, array $headers = [], $options = 0) |
− | jsonp( | + | jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0) |
− | stream( | + | stream($callback, $status = 200, array $headers = []) |
− | download( | + | download($file, $name = null, array $headers = [], $disposition = 'attachment') |
− | redirectTo( | + | redirectTo($path, $status = 302, $headers = [], $secure = null) |
− | redirectToRoute( | + | redirectToRoute($route, $parameters = [], $status = 302, $headers = []) |
− | redirectToAction( | + | redirectToAction($action, $parameters = [], $status = 302, $headers = []) |
− | redirectGuest( | + | redirectGuest($path, $status = 302, $headers = [], $secure = null) |
− | redirectToIntended( | + | redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null) |
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)