【问题标题】:Laravel: How to respond with custom 404 error depending on routeLaravel:如何根据路由响应自定义 404 错误
【发布时间】:2013-08-01 02:10:15
【问题描述】:

我正在使用Laravel4 框架,我遇到了这个问题。

我想根据请求的 url 显示自定义 404 错误。

例如:

Route::get('site/{something}', function($something){
    return View::make('site/error/404');
});

Route::get('admin/{something}', function($something){
    return View::make('admin/error/404');
});

'$something' 的值并不重要。

所示示例仅适用于一个段,即'site/foo''admin/foo'。 如果有人请求'site/foo/bar''admin/foo/bar',laravel 会抛出默认的 404 错误。

App::missing(function($exception){
    return '404: Page Not Found';
});

我试图在 Laravel4 文档中找到一些东西,但没有什么东西适合我。 请帮忙:)

谢谢!

【问题讨论】:

    标签: laravel laravel-4 http-status-code-404 laravel-routing


    【解决方案1】:

    app/start/global.php

    App::missing(function($exception) 
    {
        if (Request::is('admin/*'))
        {
            return Response::view('admin.missing',array(),404);
        }
        else if (Request::is('site/*'))
        {
            return Response::view('site.missing',array(),404);
        }
        else
        {
             return Response::view('default.missing',array(),404);
        }
    });
    

    在您看来,您可以找到$something{{ Request::path(); }}{{ Request::segment(); }}

    【讨论】:

    猜你喜欢
    • 2016-08-04
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2013-12-26
    • 2014-12-21
    • 2017-10-20
    • 1970-01-01
    相关资源
    最近更新 更多