Akil Patel
Akil Patel

Reputation: 185

Custom 404 laravel

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.

Upvotes: 3

Views: 8692

Answers (4)

DukesNuz
DukesNuz

Reputation: 87

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');
   });

Upvotes: 1

user5446912
user5446912

Reputation:

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.

Upvotes: 3

newbdeveloper
newbdeveloper

Reputation: 394

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

Upvotes: 0

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

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'); 
}

Upvotes: 7

Related Questions