Laravel:如何响应自定义404错误(取决于路由) [英] Laravel: How to respond with custom 404 error depending on route

查看:436
本文介绍了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(); }}

这篇关于Laravel:如何响应自定义404错误(取决于路由)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆