开发者

Zend Framework - Redirecting to IndexController in different module

All,

I have the following project setup in Zend's mvc Framework. Upon accessing the application, I want the user to redirect to "mobile" module instead of going to IndexController in "default" module. How do I do that?

-billingsystem
-application
    -configs
        application.ini
    -layouts
        -scripts
            layout开发者_JS百科.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

My index.php has the following code:

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


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

$application->bootstrap()->run();


you have two options :

1- set mobile as default module for your application by editing your application.ini file resources.frontController.defaultModule = "mobile"

2- you can create a plugin that intercept every request and forward to the same controller and action but to the mobile module

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 ;
                 }
    }
}

and don't forget to add the error controller to the if clause so you don't end up with mysterious loop when your application throws an error , and that is it


From within a controller you you can use

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

For example:

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

This would go to the index action, index controller of the mobile module, you can also use null:

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

Passing null will use the current action and controller.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜