Zend Framework - 重定向到不同模块中的 IndexController


Zend Framework - Redirecting to IndexController in different module

All,

我在Zend的mvc框架中有以下项目设置。访问应用程序后,我希望用户重定向到"移动"模块,而不是转到"默认"模块中的索引控制器。我该怎么做?

-billingsystem
-application
    -configs
        application.ini
    -layouts
        -scripts
            layout.phtml
    -modules
        -default
            -controllers
                IndexController.php
            -models
            -views
            Bootstrap.php
        -mobile
            -controllers
                IndexController.php
            -models
            -views
            Bootstrap.php
Bootstrap.php
-documentation
-include
-library
-logs
-public
    -design
        -css
        -images
        -js
    index.php
    .htaccess

我的索引.php具有以下代码:

require_once 'Zend/Application.php';
require_once 'Zend/Loader.php';

$application = new Zend_Application(
  APPLICATION_ENV,
  APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()->run();

您有两个选择:

1-通过编辑应用程序.ini文件,将mobile设置为应用程序的默认模块 resources.frontController.defaultModule = "mobile"

2-您可以创建一个插件来拦截每个请求并转发到相同的控制器和操作,但转发到移动模块

class Name_Controller_Plugin_mobile extends Zend_Controller_Plugin_Abstract {
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
                $module = $request->getModuleName();
                $controller = $request->getControllerName();
                $action = $request->getActionName();
                if($module !== "mobile" || $module !== "error"){
                  return $request->setModuleName("mobile")->setControllerName("$controller")
                    ->setActionName("$action")->setDispatched(true);
                 }else{
                   return ;
                 }
    }
}

并且不要忘记将错误控制器添加到 if 子句中,这样当您的应用程序抛出错误时,您就不会以神秘循环结束,仅此而已

在控制器中

您可以使用
 _forward(string $action, 
          string $controller = null, 
          string $module = null, 
          array $params = null)

例如:

$this-_forward("index", "index", "mobile");

这将转到索引操作,移动模块的索引控制器,也可以使用null

$this-_forward(null, null, "mobile");

传递null将使用当前操作和控制器。

希望有帮助。