简体   繁体   English

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

[英]how to show 404 page if route not found in laravel

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



});

update 06.08.2020 2020 年 8 月 6 日更新

Make a 404.blade.php page in /resources/views/errors/ folder and that page will be shown if:/resources/views/errors/文件夹中创建一个 404.blade.php 页面,该页面将在以下情况下显示:

  • a route does not exist路线不存在
  • when you use ->findOrFail($modelId);当你使用->findOrFail($modelId);
  • when you use abort(404);当你使用abort(404);

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

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

You can use the abort();您可以使用 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. Laravel 会自动在那里获取错误页面并显示它。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM