【问题标题】:Phalcon forward to different modulePhalcon 转发到不同的模块
【发布时间】:2014-07-26 11:25:32
【问题描述】:

尝试通过设置 phalcon mvc 应用程序来工作。

我当前设置了 2 个模块用于测试。 “前端”和“管理员”。

我设置了不同的视图,因此我可以确认我正在完成每个模块。当我更改defaultnamespacedefaultmodule 时,我确实可以看到两个模块都可以正常访问并且加载正常。我可以看到管理控制器正在被正确访问,并且当我更改它时前端控制器正在被访问。

我目前遇到的问题是,当我尝试对用户进行身份验证并启动会话时,我想将请求从“前端”转发到“管理员”:

return $this->dispatcher->forward(array(
                'namespace' => 'Qcm\Admin\Controllers',
                'action' => 'index',
                'controller' => 'index'
            ));

我再次确认这些命名空间可以正常工作。问题是当我现在转发到新的命名空间时,它再也找不到管理索引控制器了?

"Qcm\Admin\Controllers\IndexController handler class cannot be loaded"

但是我已经确认可以通过更改defaultnamespace/defaultmodule 在模块之间切换。这是调度程序内部的限制,我无法转发到其他模块吗?

只是为了澄清我也在使用相同的 url,所以例如在登录后我希望它返回到“/”(root),但是因为它已转发到管理模块,这应该可以正常工作吗?

【问题讨论】:

  • 谢谢,但我已经浏览了该主题和其中的相关链接,但无论这是否可能,似乎都没有明确的答案。临时重定向似乎也不正确,因为它只会弹回“/”(root)而不转发到管理模块?

标签: phalcon phalcon-routing


【解决方案1】:

phalcon 调度程序只能转发到同一模块内的操作。它无法将您转发到当前模块之外。之所以出现这种限制,是因为调度程序只知道声明它的模块。

为了转发到另一个模块,您必须改为从控制器操作返回重定向响应。就我而言,我想根据插件的 beforeDispatch() 方法中的 ACL 权限将用户转发到登录屏幕或 404 错误页面。调度程序是此方法的本机,但不能将用户转发到当前模块之外。相反,我让调度程序将用户转发到同一模块中的控制器,该控制器具有自定义操作,该操作反过来执行重定向。

// hack to redirect across modules
$dispatcher->forward(
    array(
        'controller'   => 'security',
        'action'       => 'redirect',
        'params'       => array(
            'redirect' => '/home/index/login'
        ),
    )
);
return false; // stop progress and forward to redirect action

这意味着每个模块都需要在其一个控制器中拥有此自定义重定向操作的副本。我通过将动作放在我的所有控制器扩展的基本控制器中来实现这一点。

/**
 * the phalcon dispatcher cannot forward across modules
 * instead, forward to this shared action which can then redirect across modules
 * */
public function redirectAction(){
    $this->view->disable();
    $params = $this->dispatcher->getParams();
    $redirect = '/';
    if( ! empty( $params['redirect'] ) ){
        $redirect = $params['redirect'];
    }
    return $this->response->redirect( $redirect );
}

【讨论】:

  • 注意必须添加redirect函数的acl
【解决方案2】:

因为 phalcon 没有将所有模块添加到全局加载器,所以命名空间没有注册。您需要在当前模块引导文件中注册另一个模块,将您的 Module.php 修改为

class Module
{
    public function registerAutoloaders()
    {
        $loader = new \Phalcon\Loader();

        $loader->registerNamespaces(array(
             //Your current module namespaces here
             ....
             //Another module  namespaces here
             'Qcm\Admin\Controllers' => 'controller path',
        ));

        $loader->register();
    }
}

【讨论】:

    【解决方案3】:

    它显示 IndexController 类未加载的主要原因是您可能没有在该模块或引导文件中添加 Dispatcher(取决于您的方法)

    添加

    $debug = new \Phalcon\Debug();
    $debug->listen();
    

    到你的代码之前

    $application->handle()->getcontent();
    

    查看错误。

    【讨论】:

      【解决方案4】:

      替换行之前:

       echo $application->handle()->getContent();
      

      代码:

          $router  = $this->getDi()->get("router");
          $params  = $router->getParams();
          $modName = $router->getModuleName();
          $url     = null;
      
          if ($modName == "admin" && isset($params[0])) {
              $module     = "/" . $params[0];
              $controller = isset($params[1]) ? "/" . $params[1] . "/" : null;
              $action     = isset($params[2]) ? $params[2] . "/" : null;
              $params     = sizeof($params) > 3 ? implode("/", array_slice($params, 3)) . "/" : null;
              $url        = $module . $controller . $action . $params;
          }
      echo $application->handle($url)->getContent();
      

      【讨论】:

      • $this->add('/admin/:params', [ 'module' => "admin", 'params' => 1 ])->setName('adminMod');
      猜你喜欢
      • 2014-12-29
      • 2014-07-15
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多