Praveen Srinivasan
Praveen Srinivasan

Reputation: 1620

Laravel forward to 404 error page

I am using Laravel 5.0. Is there any way to forward to default 404 page error if the user wrong url. I want to forward all the time if the user entered wrong url in my project.

Upvotes: 0

Views: 1658

Answers (1)

The Alpha
The Alpha

Reputation: 146191

In your resources/views/errors folder make sure you have a 404.blade.php file and if that is not there then create it and put something in this file.

Basically, if that 404 file is not present there then You'll see an error like this:

Sorry, the page you are looking for could not be found.

NotFoundHttpException in Application.php line 901:

....

.

Sepending on your environment setup. FYI, in app\Exceptions\Handler.php file the handler method handles/catches the errors. So check it, you may customize it, for example:

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

// You can use/catch this but basically this is not included in Handler
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class Handler extends ExceptionHandler {

    //...

    public function render($request, Exception $e)
    {
        // Customize it, extra code
        if ($e instanceof AccessDeniedHttpException) {
            return response(view('errors.403'), 403);
        }
        
        // The method has only this line by default
        return parent::render($request, $e);
    }
}

Then, make sure, the 403.blade.php is also available in your resources/views/errors directory.

Upvotes: 2

Related Questions