user9014687
user9014687

Reputation: 57

Redirect Error 404 in Laravel

I have a question: how I redirect in Laravel from a 404 error to a route??? This is the route: Route::get("/errorpage","Controller@errorpage")->name("errorpage");

Upvotes: 1

Views: 1304

Answers (1)

PedroFaria99
PedroFaria99

Reputation: 484

For that, you need to do add few lines of code to render method in app/Exceptions/Handler.php file which looks like this:

public function render($request, Exception $e)
    {
        if($this->isHttpException($e))
        {
            switch ($e->getStatusCode()) 
                {
                // not found
                case 404:
                return redirect()->guest('/errorpage');
                break;

                default:
                    return $this->renderHttpException($e);
                break;
            }
        }
        else
        {
                return parent::render($request, $e);
        }
    }

Upvotes: 1

Related Questions