Create Custom 404 Error Page You need to create blade views for error pages, move to this path resources/views/ inside here create errors folder and within the directory create 404. blade. php file. It will redirect you to the 404 page if you don't find the associated URL.
Default 404 Page: blade. file inside that folder. So, basically laravel will stetted default design, but if you created 404 file into "errors" directory then it will take from there.
Go to resources/views/errors and create a 404.blade.php file with what you want on your 404 page and Laravel takes care of the rest.
if you want to have some global solution, you can do changes in /app/Exceptions/Handler.php by adding code bellow
public function render($request, Exception $e)
{
if ($this->isHttpException($e)) {
$statusCode = $e->getStatusCode();
switch ($statusCode) {
case '404':
return response()->view('layouts/index', [
'content' => view('errors/404')
]);
}
}
return parent::render($request, $e);
}
In Laravel 5 you could simply put a custom 404.blade.php under resources/views/errors and that's it. For other errors like 500 you could try the following in your app/Exeptions/Handler.php:
public function render($request, Exception $e)
{
if ( ! config('app.debug') && ! $this->isHttpException($e)) {
return response()->view('errors.500');
}
return parent::render($request, $e);
}
And do the same for 500 HTTP Exeptions
I like the case statement approach but it has some issues going levels deep.
However, this catches all errors:
Route::any('/{page?}',function(){
return View::make('errors.404');
})->where('page','.*');
Laravel 5 already has a pre-defined render method(line 43) under app/Exceptions/Handler.php. Simply insert the redirection code before parent::render. Like so,
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException)
{
$e = new NotFoundHttpException($e->getMessage(), $e);
}
//insert this snippet
if ($this->isHttpException($e))
{
$statusCode = $e->getStatusCode();
switch ($statusCode)
{
case '404': return response()->view('error', array(), 404);
}
}
return parent::render($request, $e);
}
Note: My view is under resources/views. You can somehow put it anywhere else you want.
Lavavel 5.8
Create a file in resources/views/errors/404.blade.php
and add this code.
@extends('errors::minimal')
@section('title', __('Not Found'))
@section('code', '404')
@if($exception)
@section('message', $exception->getMessage())
@else
@section('message', __('Not Found'))
@endif
Then in your controller you can use:
abort(404, 'Whatever you were looking for, look somewhere else');
I was looking for an answer to something else but thought I'd help in case someone else is looking for this.
run the command php artisan vendor:publish you will see all publishable files. type in the number for laravel-errors the error files are then published to resources/views/errors folder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With