我正在学习 Phalcon(在多模块应用程序模板中尝试 REST API),并且我对每个请求进行了简单的检查,例如“此请求是否包含特定的标头” x-api-key
(类似于 ASP.NET MVC 中的 ActionFilters)。
我尝试使用
annotations
,plugins
,beforeExecuteRoute
和beforeException
. 但是当我写其中一个时,throw new \Exception("Some exception", 500);
Phalcon 返回一个空白页,没有异常消息和代码。AFAIK 这是一个已知的错误。我试图用
dispatcher
in做到这一点beforeException
:
public function beforeException($event, $dispatcher, $exception)
{
if ($exception instanceof \Phalcon\Http\Request\Exception)
{
$dispatcher->forward(
array(
'controller' => 'error',
'action' => 'showInternalServerError'
)
);
return false;
}
//...
}
看来这很有效,但这不是一个优雅的解决方案,我对此太懒了:)
问题:你有什么更好的想法如何在 PhalconPHP 中做 ActionFilters 吗?