我正在尝试处理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中。
总结一下,问题:
提前感谢任何能够解释这两个问题的人