Redirect p/new page when 404 error occurs using Laravel 5.6

Asked

Viewed 952 times

2

What is the correct way for Laravel 5.6 to redirect another route if it is error 404?

I created the folder Resources/views/errors/ and the file 404.blade.php, and worked right, but do not want to display the error, I want to be redirected to another page.

1 answer

2


You can create a script inside the page 404.blade.php to make this redirect in this way:

<script>
window.location = "http://www.yoururl.com";
</script>

Or change the file app/Exceptions/Handler.php of Laravel, on the line containing the following code snippet:

return response()->view('core.layout::error', ['code' => 404, ...

Where you can put the route you want in place of 'core.layout::error'.

  • 1

    I did not find this line, I think that in version 5.6 no longer exists. I will keep the same JS. Thanks Wictor!

  • 1

    @Luisalberto and Wictor, this may help clarify a little more: https://answall.com/questions/232812/por-que-minhas-p%C3%a1ginas-de-erro-personalizada-n%C3%a3o-est%C3%a3o-sendo-chamadas/232822#232822 . It’s in that file that Wictor said you can put that JS

  • Miguel, that’s exactly what I did, but I think I’d be better off using Laravel’s system. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.