Laravel 4异常处理 - 相同的代码在1种情况下工作,而在其他2种情况下不起作用

时间:2013-06-24 21:38:21

标签: laravel laravel-4

我正在尝试处理Laravel 4中的一些例外,并且相同的代码片段适用于其中一个但不适用于其他代码。

我声明了一个我添加到composer.json的Exceptions.php文件。

在Exceptions.php中,我声明了以下内容:

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Class NotAllowedException extends Exception {};

Exceptions.php中的以下处理程序正常工作:

// snippet 1
App::error(function(NotAllowedException $e, $code, $needed)
{
return Response::make(View::make('special.error')->with('error', array('headline' => 'Not Allowed',
                          'description' =>'<p>Your user has not enough privileges to perform the requested operation.</p>')), 401);

});

当我尝试同样处理HttpNotFoundExceptions和ModelNotFoundExceptions时,以下2个片段作为第一个片段的副本不起作用并产生未定义变量:错误错误。

// snippet 2
App::error(function(ModelNotFoundException $e)
{
return Response::make(View::make('special.error')->with('error', array('headline' =>'Not Found',
                'description' => '<p>It seems you have tried to access a page that does not exist.</p>')), 404);
});


// snippet 3
App::error(function(NotFoundHttpException $e)
{
return Response::make(View::make('special.error')->with('error', array('headline' =>'Not Found',
                'description' => '<p>It seems you have tried to access a page that does not exist.</p>')), 404);
});

我只能通过将它放入global.php来使NotFoundHttpException工作:

App::missing(function($exception)
{
return Response::make(View::make('special.error')->with('error', array('headline' =>'Not Found',
                'description' => '<p>It seems you have tried to access a page that does not exist.</p>')), 404);
});

但是如果我把它放在那里我不知道它为什么会起作用,如果把它放在Exceptions.php中它就不起作用。

在内部服务器错误的情况下,尝试将片段2和3放在global.php中。

总结一下,问题

  • 我认为我按照Laravel4的文档进行ModelNotFoundException处理;我做错了什么/如何使它发挥作用?
  • 为什么App :: missing只有在我把它放在global.php中才能工作?我担心我可能会遗漏一些关于Laravel内部运作的重要内容。

提前感谢任何能够解释这两个问题的人

0 个答案:

没有答案