Laravel 路由和 404 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20474556/
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 routing and 404 error
提问by spacemonkey
I want to specify that if anything entered in the url address other than existing routes (in routes.php, then show 404 page.
我想指定,如果在 url 地址中输入了除现有路由之外的任何内容(在 routes.php 中,则显示 404 页面。
I know about this:
我知道这个:
App::abort(404);
but how can I specify the part where everything else except the routes defined?
但是如何指定除路由之外的所有其他内容的部分?
回答by Antonio Carlos Ribeiro
You can add this to your filters.php file:
您可以将其添加到您的 filters.php 文件中:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
And create the errors.missing
view file to show them the error.
并创建errors.missing
视图文件以向他们显示错误。
Also take a look at the Errors & Logging docs
另请查看错误和日志记录文档
EDIT
编辑
If you need to pass data to that view, the second parameter is an array you can use:
如果您需要将数据传递给该视图,则第二个参数是您可以使用的数组:
App::missing(function($exception)
{
return Response::view('errors.missing', array('url' => Request::url()), 404);
});
回答by Josh Mountain
I would advice putting this into your app/start/global.php
as that is where Laravel handles it by default (though filters.php
will also work). I usually use something like this:
我建议把它放到你的app/start/global.php
因为 Laravel 默认处理它的地方(虽然filters.php
也可以工作)。我通常使用这样的东西:
/*
|--------------------------------------------------------------------------
| Application Error Handler
|--------------------------------------------------------------------------
|
| Here you may handle any errors that occur in your application, including
| logging them or displaying custom views for specific errors. You may
| even register several error handlers to handle different types of
| exceptions. If nothing is returned, the default error view is
| shown, which includes a detailed stack trace during debug.
|
*/
App::error(function(Exception $exception, $code)
{
$pathInfo = Request::getPathInfo();
$message = $exception->getMessage() ?: 'Exception';
Log::error("$code - $message @ $pathInfo\r\n$exception");
if (Config::get('app.debug')) {
return;
}
switch ($code)
{
case 403:
return Response::view('errors/403', array(), 403);
case 500:
return Response::view('errors/500', array(), 500);
default:
return Response::view('errors/404', array(), $code);
}
});
Then just make an errors
folder inside /views
and place your error page content there. As Antonio mentioned you can pass data inside the array()
.
然后只需errors
在里面创建一个文件夹/views
并将您的错误页面内容放在那里。正如安东尼奥所说,您可以在array()
.
I kindly borrowed this method from https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site
我好心地从https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site借用了这个方法
回答by Indy
Just for guys using Laravel 5, there is an errors folder in in the views directory. You just need to create a 404.blade.php file there and it will render this view when there is no route specified for a url
对于使用 Laravel 5 的人来说,views 目录中有一个错误文件夹。您只需要在那里创建一个 404.blade.php 文件,它就会在没有为 url 指定路由时呈现此视图
回答by manix
This is my approach just for displaying the error 404 with a template layout. Just add the following code to /app/start/global.php
file
这是我的方法,仅用于使用模板布局显示错误 404。只需将以下代码添加到/app/start/global.php
文件中
App::missing(function($exception)
{
$layout = \View::make('layouts.error');
$layout->content = \View::make('views.errors.404');
return Response::make($layout, 404);
});