phalcon forward() 在辅助函数中不起作用

phalcon forward() doesnt work from helper function

我想在我的辅助函数中使用 forward() 函数。可惜我做不到。

索引控制器:

class IndexController extends ControllerBase

   public function indexAction()
      $helper = new CustomFunctions();
      $helper->log();
      die('still here');

   {
{

自定义函数

class CustomFunctions extends \Phalcon\DI\Injectable{
   public function log(){
      //...
      $this->dispatcher->forward([
            'controller' => 'error',
            'action'     => 'route404'
        ]);

        return false;
   }

}

为什么转发不起作用?它应该将我转发到错误控制器而不是显示来自索引控制器的消息。谢谢大家的建议。

dispatcher->forward 在 Phalcon 中不是那样工作的。它只是一个 shorthand 用于启动另一个控制器和动作,但它不会退出或 return 从当前控制器或动作。换句话说,您可能应该检查 $helper->log(); return 是否为假,如果是,则假设 return;exit; 即使您使用重定向而不是转发,这意味着它向客户端的浏览器发送一个 header ,它仍然通过 Phalcon 并且仅在 Phalcon 被告知发送 header 之后才发送。这允许您围绕 Phalcon 中的事物编写测试,这与实际立即使用 header 设置事物不同。不立即退出允许您测试 ->forward->redirect 之类的东西,而无需在中间停止测试。如果您需要模拟提前退出,您也可以尝试 throw 方法,然后捕获这样的错误来决定是否是时候 exit;。这种方法会给你 exit; 的好处,同时仍然是可测试的。或者你可以在你已经拥有的地方扔一个骰子,等等,或者从你的错误控制器中 exit; 等等。

您可能想阅读有关调度循环的内容:
https://docs.phalconphp.com/en/3.2/dispatcher