如果在 Laravel 中找不到路由,如何显示 404 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32177178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:14:38  来源:igfitidea点击:

how to show 404 page if route not found in laravel

phplaravellaravel-5.1

提问by scott

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

我已经使用中间件创建了路由组。它工作得很好。

But i have one issue where if i navigate url to

但是我有一个问题,如果我将 url 导航到

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

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

但如果我将 url 导航到

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

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

在 url 中没有管理员然后它返回空白页面。现在我的问题是如何显示页面未找到 404 页面。我正在使用 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'); });



});

回答by lewis4u

update 13.11.2017

2017 年 11 月 13 日更新

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

只需在/resources/views/errors/文件夹中创建 404.blade.php 页面,如果路由不存在,将显示该页面



instead of laravel error for non existing route:

而不是非现有路线的laravel错误:

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
  1. /resources/views/errors/文件夹中创建 404.blade.php 页面

and then just call it with

然后就用

abort(404);

For example make a route like this:

例如制作这样的路线:

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

回答by Imtiaz Pabel

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

您使用此设置了 404 路由。然后使用该路由中的任何视图文件

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

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

回答by CDF

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

您可以使用 abort(); 方法并在错误文件夹中创建一个视图,正如文档中所述。

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

Laravel 会自动在那里获取错误页面并显示它。

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

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

回答by Nick Nick

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