7
votes

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



});
5
There is no error thrown?Try to add the 404 error page at resources/views/errors/404.blade.php and see if the page is rendered..Mihai Matei
@MateiMihai.no error thrown.i have added 404 error page but still not working.also i have that add-post-new page but it should be access to admin only not for any other usersscott
Please tell us how the route group looks like (code). Otherwise it is just guessing.barfoos
@martinweise.i have update my questionscott
How to handle this for rest API's? I need to send json response.151291

5 Answers

18
votes

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);
});
2
votes

you set an 404 route using this.then use any view file in that route

App::error(function(Exception $exception, $code)
{
Log::error($exception);

if (Config::get('app.debug') == false) {
    return Redirect::route('404');
}
});
0
votes

You can use the abort(); method and create a view into the error folder, just as explained in the documentation.

Laravel will automatically fetch the error page there and display it.

http://laravel.com/docs/5.1/errors#http-exceptions

0
votes
try{
    return view($SomeInvalidView);
}  catch (Exception $e){
    abort(404);
}