Php – how to show 404 page if route not found in laravel

laravellaravel-5.1PHP

i have created route group using middleware.It works perfectly.

But i have one issue where if i navigate url to

http://localhost/laravel-news/public/admin/add-post-new

this without login then it redirect to guest home page

but if i navigate url to

http://localhost/laravel-news/public/add-post-new

without admin in url then it return blank page.now my question is how to show page not found 404 page for that.i am using laravel 5.1

thank you

update

Route::group(['middleware' => 'admin'], function () {


            Route::get('add-post-new', function () {

        //  dd('something');
            return view('a.addPost');

            });

            Route::post('/add-post-new','PostsController@addPost');

            Route::get('/all-post', function () {return view('a.all_post'); });



});

Best Answer

update 06.08.2020

Make a 404.blade.php page in /resources/views/errors/ folder and that page will be shown if:

  • a route does not exist
  • when you use ->findOrFail($modelId);
  • when you use abort(404);

instead of laravel error for non existing route:

Sorry, the page you are looking for could not be found.
1/1 NotFoundHttpException in RouteCollection.php line 161:
  1. make 404.blade.php page in /resources/views/errors/ folder

and then just call it with

abort(404);

For example make a route like this:

Route::get('/404', function () {
    return abort(404);
});
Related Topic