Laravel:如何响应自定义404错误(取决于路由)

Laravel:如何响应自定义404错误(取决于路由)

问题描述:

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

I'm using Laravel4 framework and I came across this problem.

我想显示一个自定义的404错误,具体取决于请求的网址.

I want to display a custom 404 error depending on requested url.

例如:

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错误.

Shown example only works with one segment, i.e. 'site/foo' or 'admin/foo'. If someone request 'site/foo/bar' or 'admin/foo/bar' laravel will throw default 404 error.

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

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

I tried to find something in Laravel4 documentation but nothing is just right for me. Please help :)

谢谢!

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

在您看来,您可以使用{{ Request::path(); }}{{ Request::segment(); }}

In your view, you can find $something with {{ Request::path(); }} or {{ Request::segment(); }}