how to show 404 page if route not found in laravel

2019-06-25 04:06发布

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



});

4条回答
Viruses.
2楼-- · 2019-06-25 04:48

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');
}
});
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-06-25 04:52

update 13.11.2017

just make 404.blade.php page in /resources/views/errors/ folder and that page will be shown if a route does not exist


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);
});
查看更多
欢心
4楼-- · 2019-06-25 04:57

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

查看更多
The star\"
5楼-- · 2019-06-25 04:58
登录 后发表回答