2
votes

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.

5

5 Answers

7
votes

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

6
votes

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

0
votes

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?>
0
votes

On the off chance that you figure how might I make custom error page for example 403, 500, 502, 504, 404, 405, 408, on your php laravel application then you can make effectively and execute it without any problem. You can set custom blunder page universally in laravel venture. on the off chance that you need to make 404 page your customize error page, at that point you can do it without any problem.

app/Exceptions/Handler.php

namespace App\Exceptions;

 

use Exception;

use Illuminate\Validation\ValidationException;

use Illuminate\Auth\Access\AuthorizationException;

use Illuminate\Database\Eloquent\ModelNotFoundException;

use Symfony\Component\HttpKernel\Exception\HttpException;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

 

class Handler extends ExceptionHandler

{

 

/**

* A list of the exception types that should not be reported.

*

* @var array

*/

protected $dontReport = [

AuthorizationException::class,

HttpException::class,

ModelNotFoundException::class,

ValidationException::class,

];

 

/**

* Report or log an exception.

*

* This is a great spot to send exceptions to Sentry, Bugsnag, etc.

*

* @param \Exception $e

* @return void

*/

public function report(Exception $e)

{

parent::report($e);

}

 

/**

* Render an exception into an HTTP response.

*

* @param \Illuminate\Http\Request $request

* @param \Exception $e

* @return \Illuminate\Http\Response

*/

public function render($request, Exception $e)

{

if($this->isHttpException($e)){

if (view()->exists('errors.'.$e->getStatusCode()))

{

return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode());

}

}

return parent::render($request, $e);

}

}

you can check this from here

0
votes

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.