Reputation: 1680
I want to show custom error page in my Laravel 5 app; for example any user type URL like http://www.example.com/url123
(wrong) but http://www.example.com/url
(right).
Default error show as :
Uh-oh, something went wrong! Error Code: 500
But instead I want to show my custom view
How I can achieve that as illustrated in the pages referenced below:
https://mattstauffer.co/blog/laravel-5.0-custom-error-pages#how-to
https://laracasts.com/discuss/channels/general-discussion/how-do-i-create-a-custom-404-error-page
My current app/Exceptions/Handler.php
:
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use symfony\http-kernel\Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler {
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
];
public function report(Exception $e)
{
return parent::report($e);
}
public function render($request, Exception $e)
{
if ($this->isHttpException($e))
{
return $this->renderHttpException($e);
}
else if($e instanceof NotFoundHttpException)
{
return response()->view('missing', [], 404);
}
else
{
return parent::render($request, $e);
}
}
}
And I've create a error view at \resources\views\errors\404.blade.php
but 404.blade.php
still does not get loaded.
Upvotes: 4
Views: 31913
Reputation: 149
first you need to type command php artisan vendor:publish --tag=laravel-errors
It will create some error blade files in resources/views/errors such as 404,500,403,401,419,429,503. You can customize these pages as per your design requirement.
app/Exceptions/Handler.php
public function render($request, Throwable $exception)
{
$response = parent::render($request, $exception);
if ($response->status() === 500) {
return response(view('errors.500'), 500);
}
return $response;
}
app/config/app.php
'debug' => (bool) env('APP_DEBUG', false)
Note: Do not create errors folder manually in view folder because some library files not creating in vendor folders. Its the reason to throw the error. please only do with the laravel command.
Upvotes: 1
Reputation: 1680
Thanks guys, Now it is working successfully,
I just change my app/Exceptions/Handler.php :
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler {
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
];
public function report(Exception $e)
{
return parent::report($e);
}
public function render($request, Exception $e)
{
if ($this->isHttpException($e)) {
return $this->renderHttpException($e);
} else {
return parent::render($request, $e);
}
}
}
and create a error view on : \resources\views\errors\404.blade.php
Upvotes: 7
Reputation: 868
Im using Laravel 6.4 and for that, You can create custom error pages blade files and Laravel will do the rest for you. Run the following command in your project directory
php artisan vendor:publish --tag=laravel-errors
This will create blade files in resources/views/errors/.
eg for the 404 ERROR you will have it in resources/views/errors/404.blade.php.
.
Now Let's say you wish to pass a Not Found Error in your sample code lets say a function to return a post category
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$count = Post::find($id)->count();
if ($count < 1) return abort('404', 'The post you are looking for was not found');
}
The abort()
method will trigger a not found exception loading the blade file resources/views/errors/404.blade.php
and passing in the second parameter as the message. You can access this message in the blade file as
<h2>{{ $exception->getMessage() }}</h2>
Go to this link(official documentation) for details https://laravel.com/docs/6.x/errors#custom-http-error-pages
Upvotes: 10
Reputation: 4353
This worked for me in Laravel 5.5:-
/config/constants.php
define('ERROR_MSG_403', "You are not authorized to view that page!");
define('ERROR_MSG_404', "Page not found!");
define('ERROR_MSG_UNKNOWN', "Something went wrong!");
/app/Exceptions/Handler.php
public function render($request, Exception $e)
{
$response = [];
$response['exception'] = get_class($e);
$response['status_code'] = $e->getStatusCode();
switch($response['status_code'])
{
case 403:
$response['message'] = ERROR_MSG_403;
break;
case 404:
$response['message'] = ERROR_MSG_404;
break;
default:
$response['message'] = ERROR_MSG_UNKNOWN;
break;
}
return response()->view('Error.error', compact('response'));
// return parent::render($request, $exception);
}
/resources/views/Error/error.blade.php
<?=dd($response); //Implement your view layout here?>
Upvotes: 0