1

尝试通过设置 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。这是调度程序中的限制,我无法转发到其他模块吗?

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

4

4 回答 4

2

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 );
}
于 2014-10-14T06:11:17.610 回答
1

因为 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();
    }
}
于 2014-07-27T06:32:11.340 回答
0

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

添加

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

之前的代码

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

查看错误。

于 2014-07-30T09:44:57.077 回答
0

换行前:

 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();
于 2017-10-11T19:32:23.017 回答