Laravel 4 处理 404 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17661856/
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 4 handling 404 errors
提问by user1827863
I am new to Laravel and I'm trying to catch any requests that do not match existing routes. Currently I am getting a ...
我是 Laravel 的新手,我正在尝试捕获任何与现有路由不匹配的请求。目前我得到一个...
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
I have created an App::missing()
handler in the app/start/global.php
as described on the documentation pagebut that doesn't help. I also tried creating an App::error(function(NotFoundHttpException $exception, $code)
handler, but that didn't help either.
我已经按照文档页面上的描述创建了一个App::missing()
处理程序,但这没有帮助。我也尝试创建一个处理程序,但这也无济于事。app/start/global.php
App::error(function(NotFoundHttpException $exception, $code)
Any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by fideloper
That's likely due to debugging being turned on in the app/config/app.php
file. Try turning that value to false
and see if your custom handler than 'handles" it.
这可能是由于在app/config/app.php
文件中打开了调试。尝试将该值转换为false
并查看您的自定义处理程序是否比“处理”它。
Additionally, your error handler needs to return a Response in order to "short-circuit" the request and actually respond with the result of your error handler - Are you returning a value / Response class from your App::error()
handler? (Please show us that code).
此外,您的错误处理程序需要返回一个 Response 以“短路”请求并实际响应您的错误处理程序的结果 - 您是否从您的App::error()
处理程序返回值/响应类?(请向我们展示该代码)。
Here's an article on Laravel 4 Error Handlingwithout outlines the process of how Laravel uses App::missing()
and App::error()
handlers when errors occur.
这里有一篇关于Laravel 4 错误处理的文章,没有概述 Laravel在错误发生时如何使用App::missing()
和App::error()
处理程序的过程。
Pay specific attention to the "meat of the Handler class" section - it outlines how you need to return a value of some sort from the handler in order for it not to pass the Exception to the next handler (Likely your Whoops error output that displays when debug
is set to true
in app/config/app.php
).
特别注意“处理程序类的内容”部分 - 它概述了您需要如何从处理程序返回某种类型的值,以便它不会将异常传递给下一个处理程序(可能是您显示的 Whoops 错误输出whendebug
设置为true
in app/config/app.php
)。
回答by Lajdák Marek
You need this: In 'app/start/global.php' add:
你需要这个:在“app/start/global.php”中添加:
App::missing(function($exception)
{
return Response::view('errorView', array(), 404);
});
And of course in views create (in this case) errorView.blade.php
EDIT: This method handle all "404 Not Found" errors.
当然在视图中创建(在这种情况下)errorView.blade.php
编辑:此方法处理所有“404 Not Found”错误。
回答by Samuel De Backer
Here is how to throw a 404 error when a ModelNotFoundException occurs
这是发生 ModelNotFoundException 时如何抛出 404 错误
App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception, $code)
{
return Response::view('errors.404', array(), 404);
});
回答by Sagiruddin Mondal
in
在
app -> config -> app.php
应用程序 -> 配置 -> app.php
if you make
如果你做
'debug' => true,
to this
对此
'debug' => false,
You will get an user experience environment which is other than the debugging mode. Please make sure you have checked it.
您将获得除调试模式之外的用户体验环境。请确保您已检查过。
Thanks :)
谢谢 :)
回答by kintso
I was able to make it works using the following code. I let 'debug' => true, in my local configuration.
我能够使用以下代码使其工作。我在本地配置中让 'debug' => true。
In app/start/global.php
在 app/start/global.php
App::error(function(Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception)
{
Log::error($exception);
$response = Response::make(View::make('response')
->with('data', null)
->with('meta', 'Error, incorrect path')
->with('pagination', null));
$response->setStatusCode(404);
$response->header('content-type', 'application/json', true);
return $response;
});
If of any interest, the content of my view is
如果有任何兴趣,我的观点的内容是
<?php
echo json_encode(array(
'data' => $data,
'pagination' => $pagination,
'meta' => $meta), JSON_PRETTY_PRINT);
?>
The main challenge was to use Symfony\Component\HttpKernel\Exception\NotFoundHttpException as the type of the Exception and not only NotFoundHttpException.
主要的挑战是使用 Symfony\Component\HttpKernel\Exception\NotFoundHttpException 作为 Exception 的类型,而不仅仅是 NotFoundHttpException。
By using NotFoundHttpException, a 500 error was thrown.
通过使用 NotFoundHttpException,抛出了 500 错误。