Skip to content

Instantly share code, notes, and snippets.

@ishikawam
Last active May 18, 2019 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishikawam/98db18ef55b8d8146f6d4707ed8d026b to your computer and use it in GitHub Desktop.
Save ishikawam/98db18ef55b8d8146f6d4707ed8d026b to your computer and use it in GitHub Desktop.
Laravel5: エラーページを共通化〜どんなステータスコードでもどんと来い! ref: https://qiita.com/M_Ishikawa/items/1f0d72fc93286109464e
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
</head>
<body>
@php
$status_code = $exception->getStatusCode();
$message = $exception->getMessage();
if (! $message) {
switch ($status_code) {
case 400:
$message = 'Bad Request';
break;
case 401:
$message = '認証に失敗しました';
break;
case 403:
$message = 'アクセス権がありません';
break;
case 404:
$message = '存在しないページです';
break;
case 408:
$message = 'タイムアウトです';
break;
case 414:
$message = 'リクエストURIが長すぎます';
break;
case 419:
$message = '不正なリクエストです';
break;
case 500:
$message = 'Internal Server Error';
break;
case 503:
$message = 'Service Unavailable';
break;
default:
$message = 'エラー';
break;
}
}
@endphp
<h1>{{ $status_code }} {{ $message }}</h1>
</body>
</html>
resources/views/errors/404.blade.php
resources/views/errors/404.blade.php
resources/views/errors/500.blade.php
resources/views/errors/503.blade.php
...
{{ http_response_code() }}
@php $guzzle = new GuzzleHttp\Psr7\Response; echo($guzzle->getStatusCode()); @endphp
/**
* 共通エラーページ
*/
protected function renderHttpException(\Symfony\Component\HttpKernel\Exception\HttpException $e)
{
$status = $e->getStatusCode();
return response()->view("errors.common", ['exception' => $e], $status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment