Laravelの標準のエラーページをカスタマイズするには
/resources/views/errors/404.blade.php にファイルを置くだけです。
ファイル名は
403.blade.php
404.blade.php
500.blade.php
とすると、404エラーの時は 「404.blade.php」を見に行きます。
abort( 404, '独自のエラーメッセージ' );
{{ $exception->getMessage() }}
これだけで、エラーページにメッセージが表示されます。
デフォルトのデザイン ↑ にしたい場合は次のようなコードにします
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page Not Found</title>
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">
<style>
html,
body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
font-weight: 100;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.content {
text-align: center;
}
.title {
font-size: 36px;
padding: 20px;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title">
Sorry, the page you are looking for could not be found.
</div>
</div>
</div>
</body>
</html>
以下のようにすると .env で APP_DEBUG=1 の時だけエラーメッセージを表示させることができます。
@if(env('APP_DEBUG') == 1)
{{ $exception->getMessage() }}
@endif
例: