30

i made simple resume site with laravel 5.4 and i wanna if users type anything except my site name and my site.com/panel automatically redirect to 404 page.

how can i do that?

is there any route to do that or what?

i found this code but not use

 public function render($request, Exception $e)
{
    if ($e instanceof 
    \Symfony\Component\HttpKernel\Exception\NotFoundHttpException){

        return response(redirect(url('/')), 404);
    }
    return parent::render($request, $e);

}
Siros Fakhri
  • 2,133
  • 5
  • 20
  • 31
  • 5
    As per docs **"create a `resources/views/errors/404.blade.php`. This file will be served on all 404 errors generated by your application."** – linktoahref Apr 24 '17 at 09:11
  • 1
    thanks man.it worked :) – Siros Fakhri Apr 24 '17 at 10:07
  • @linktoahref is right, I am just adding the correct link to Laravel 5.5 documentation https://laravel.com/docs/5.5/errors and full quote where it says: `Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a resources/views/errors/404.blade.php. This file will be served on all 404 errors generated by your application. The views within this directory should be named to match the HTTP status code they correspond to.` – Matija Sep 04 '17 at 07:08

5 Answers5

98

just add abort method

 return abort(404);

it's automatically redirect to your resources/views/errors/404.blade.php

Emad Adly
  • 1,435
  • 12
  • 6
5
return abort(404);

and set your routes too for this particular action/method with get request.

Manoj
  • 1,530
  • 2
  • 13
  • 18
2

In addition, you may provide the response text:

return abort(403, 'Unauthorized action.');
Ali
  • 664
  • 3
  • 13
  • 21
1

you can create a view in

resources/views/errors/404.blade.php

and it will redirect every unexist route that you didn't include it in

 web.php

and in 404.blade file you can put this hyper link to redirect use to your home page either by choosing route name

<a href="{{ route('home') }}">Back to home</a>

or by specifying a route link directly

<a href="/">Back to home</a>
0

create this page first

resources/views/errors/404.blade.php

after that put this code on "404.blade.php" file

{{redirect()->to('/')->send()}}