3

全部,Zend Framework - 重定向到IndexController在不同的模塊

我在Zend的mvc框架中有以下項目設置。在訪問應用程序時,我希望用戶重定向到「移動」模塊,而不是轉到「默認」模塊中的IndexController。我怎麼做?

-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 

我的index.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(); 

回答

3

你有兩種選擇:

1組mobile爲您的應用程序的默認模塊通過編輯您的application.ini文件 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語句,所以你不神祕循環結束時,你的AP摺疊會拋出一個錯誤,這就是它

3

從控制器內,你就可以使用

_forward(string $action, 
      string $controller = null, 
      string $module = null, 
      array $params = null) 

例如:

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

這去索引動作,索引控制器移動模塊,你也可以使用null

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

傳遞null將使用當前的動作和控制器。

希望有所幫助。

+0

是否有沒有辦法像index.php全局做這個,而不是改變每個控制器? – Jake 2011-05-12 04:23:58

+0

在'Zend_Controller_Plugin'中執行它,以便在調度程序啓動之前完成 – 2011-05-12 08:45:22