2

If we just create a 404.blade.php page in resources/views/error it will work fine, but Auth() won't work on 404 page, to solve that if we follow the solution available on stackoverflow the Laravel auth errors will stop working. I use the following solution to do the work.

Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52
Akil Patel
  • 145
  • 2
  • 13
  • Hi, i have a hard time understanding your problem, can you please clarify it and maybe add some code. –  Jan 24 '20 at 10:37
  • yes it will not work on you 404 page if you want to use auth in 404 page then you have to manually catch the route and create your own controller – Kamlesh Paul Jan 24 '20 at 10:38
  • yeah i already have the solution. create resources/views/errors/404.blade.php (your custom page) add the below to your routes.php ``` Route::any('{catchall}', 'PageController@notfound')->where('catchall', '.*'); ``` Now in page controller use the **notfound** function ``` public function notfound() { return view('errors.404'); } ``` This wont affect the auth errors. I answered on the post, but it was deleted. – Akil Patel Jan 24 '20 at 10:42
  • @AkilPatel that's great – Kamlesh Paul Jan 24 '20 at 10:46
  • @kamlesh if you can just copy my comment as post that as answer, it will help others as well – Akil Patel Jan 24 '20 at 10:46
  • @AkilPatel added – Kamlesh Paul Jan 24 '20 at 10:51

4 Answers4

5

Create custom view

resources/views/errors/404.blade.php

in route.php

Route::any('{catchall}', 'PageController@notfound')->where('catchall', '.*');

create PageController and add this function

public function notfound() 
{ 
    return view('errors.404'); 
}

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33
  • It was very useful. Definitely run for Laravel 8. I actually used Laravel 6 and Laravel 7 and the 404 page was working on them. However, the same methods did not work in some areas in Laravel 8. Especially such an error was emerging.(Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The GET method is not supported for this route. Supported methods: PUT, DELETE.) Thanks to this method, this error was fixed in Laravel 8. – ufuk Dec 28 '20 at 11:04
2

For Laravel 5.6 and later, you can use fallback in your routes\web.php:

Route::fallback('MyController@show404');

It works as an "catch all"-route.

See docs here.

1

Not sure if this will help. Laravel has a PHP artisan command to publish error pages. Laravel Custom HTTP Error Pages

After you run this artisan command use Route:fallback() method as @KFoobar suggested. If you use a closure function, no need to use a controller. Make sure to add the below route at the ends of your routes file.

   //Fallback/Catchall Route
   Route::fallback(function () {
       return view('errors.layout');
   });

Shameless plug for my BLOG

DukesNuz
  • 87
  • 1
  • 9
0

Write below code in your Exceptions/Handler.php

if($this->isHttpException($exception)){
   if(view()->exists('errors.'.$exception->getStatusCode())){
        $code = array('status'=>$exception->getStatusCode());
          return response()->view('errors.404',compact('code'));
       }
   }

Now create a new file in your view i.e errors/404;

Now in code array you can pass dynamic values

newbdeveloper
  • 394
  • 3
  • 11