Laravel:如何根据路由响应自定义 404 错误 [英] Laravel: How to respond with custom 404 error depending on route

查看:35
本文介绍了Laravel:如何根据路由响应自定义 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想根据请求的 url 显示自定义 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(); 找到 $something}}{{ Request::segment();}}

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

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

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