Laravel:如何根据路由响应自定义 404 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17972276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: How to respond with custom 404 error depending on route
提问by bulicmatko
I'm using Laravel4framework and I came across this problem.
我正在使用Laravel4框架,但遇到了这个问题。
I want to display a custom 404 error depending on requested url.
我想根据请求的 url 显示自定义 404 错误。
For example:
例如:
Route::get('site/{something}', function($something){
return View::make('site/error/404');
});
and
和
Route::get('admin/{something}', function($something){
return View::make('admin/error/404');
});
The value of '$something'
is not important.
的值'$something'
并不重要。
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.
所示示例仅适用于一个段,即'site/foo'
或'admin/foo'
。如果有人请求'site/foo/bar'
或'admin/foo/bar'
laravel 会抛出默认的 404 错误。
App::missing(function($exception){
return '404: Page Not Found';
});
I tried to find something in Laravel4 documentation but nothing is just right for me. Please help :)
我试图在 Laravel4 文档中找到一些东西,但没有什么适合我。请帮忙 :)
Thank you!
谢谢!
回答by user1669496
In app/start/global.php
在 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);
}
});
In your view, you can find $something
with {{ Request::path(); }}
or {{ Request::segment(); }}
在您看来,您可以找到$something
与{{ Request::path(); }}
或{{ Request::segment(); }}