Search code examples
phpmodel-view-controllerzend-frameworkzend-framework-mvczend-framework-modules

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

Solution

  • 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