PhalconPHP - 在 initialize() 中重定向

Rob*_*n71 4 php phalcon

在我的项目中,我创建了操作 ajax 请求的 AjaxController。我想输入到 ajax 使用的 url 的用户得到 404 错误。在 AjaxController.php 我有:

public function initialize() {
    if (!$this->request->isAjax()) {
        return $this->response->redirect('error/show404');
    }
}
Run Code Online (Sandbox Code Playgroud)

(当然我有带有 show404Action 的 ErrorController)

它不起作用。当我在浏览器中输入 example.com/ajax 时,我从 AjaxController 中的 IndexAction 获取内容。如何修复?

yer*_*rgo 5

请尝试在beforeExecuteRoute(). initialize()顾名思义,Phalcon旨在初始化事物。您可以使用调度程序在那里调度,但不应重定向。

您可以在此处查看部分文档。列“可以停止操作吗?” 说是否可以返回响应对象以完成请求或false停止评估其他方法并编译视图。

值得一提的是,它beforeExecuteRoute()每次都在调用 action 之前执行,因此可能会触发几次,以防您在 action 之间进行转发。

public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
{
    if (!$this->request->isAjax()) {
        return $this->response->redirect('error/show404');
    }
}
Run Code Online (Sandbox Code Playgroud)