Question

I have this code:

$defaultAdmin = array ('module' => 'admin', 'controller' => 'index', 'action' => 'index' );
$routeAdmin = new Zend_Controller_Router_Route ( 'admin/:controller/:action/*', $defaultAdmin );
$router->addRoute ( 'admin', $routeAdmin );

However, if I get into this url: /admin/ it load the controller on /application/controllers/IndexController.php instead of the one in /application/modules/admin/controllers/IndexController.php

My full bootstrap is this one:

abstract class My_Application_Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    /**
     * Inicializa las características generales de la aplicación
     *
     * @param $application Zend_Application|Zend_Application_Bootstrap_Bootstrapper         
     * @return void
     */
    public function __construct($application) {
        parent::__construct ( $application );
        $autoloader = $application->getAutoloader ();
        $autoloader->registerNamespace ( 'Busca' );
        $this->_initConfig ();
        $this->_initDb ();
        $this->_initView ();
        $this->_setCache ();
        $this->_setRouter ();
        $this->_setHelpers ();
        $this->_init ();
    }
    /**
     * Método que setea el cache del sistema
     *
     * @return My_Application_Bootstrap
     */
    protected function _setCache() {
        $config = Zend_Registry::get ( 'config' );

        $metadataBack = $config->metadata_cache->frontend->toArray ();
        $metadataFront = $config->metadata_cache->backend->toArray ();

        $memcachedOpts = $config->db_cache->toArray ();

        $memcache = new My_Cache_Backend_Memcached ( $memcachedOpts );
        $metadataCache = Zend_Cache::factory ( 'Core', 'File', $metadataBack, $metadataFront );

        Zend_Registry::set ( 'Cache', $memcache );
        Zend_Registry::set ( 'MetadaCache', $metadataCache );

        My_Model_Table_Abstract::setDefaultMetadataCache ( 'MetadaCache' );
        return $this;
    }
    /**
     * Inicializa la configuración de la aplicación
     *
     * @return My_Application_Bootstrap
     */
    protected function _initConfig() {
        Zend_Registry::set ( 'config', new Zend_Config ( $this->getOptions () ) );
        return $this;
    }
    /**
     * Inicializa la(s) base(s) de datos
     *
     * @return My_Application_Bootstrap
     */
    protected function _initDb() {
        $this->bootstrap ( 'multidb' );
        $resource = $this->getPluginResource ( 'multidb' );
        $databases = Zend_Registry::get ( 'config' )->resources->multidb;
        foreach ( $databases as $name => $adapter ) {
            $db_adapter = $resource->getDb ( $name );
            Zend_Registry::set ( $name, $db_adapter );
        }
        return $this;
    }
    /**
     * Reemplaza la vista Zend_View por My_View
     *
     * @return My_Application_Bootstrap
     */
    protected function _initView() {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper ( 'viewRenderer' );
        $view = new My_View ();
        $viewRenderer->setView ( $view );
        return $this;
    }
    /**
     * Método que setea el router general del sitio
     * Todas las peticiones irán al controlador default, con excepción de las de
     * administración.
     *
     * @return My_Application_Bootstrap
     */
    protected function _setRouter() {
        $front = Zend_Controller_Front::getInstance ();
        $router = $front->getRouter ();
        /*
         * Router para entidades
         */
        $defaultSection = array ('module' => 'default', 'controller' => 'index', 'action' => 'section', 'sectionAction' => null, 'section' => null, 'id' => null, 'title' => null );
        $requiredSection = array ('id' => '\d+' );
        $routeSection = new Zend_Controller_Router_Route ( ':section/:sectionAction/:id/:title/*', $defaultSection, $requiredSection );
        $router->addRoute ( 'section', $routeSection );
        /*
         * Router para entidades sin ID
         */
        $defaultSection = array ('module' => 'default', 'controller' => 'index', 'action' => 'section', 'sectionAction' => null, 'section' => null );
        $routeSection = new Zend_Controller_Router_Route ( ':section/:sectionAction/*', $defaultSection, $requiredSection );
        $router->addRoute ( 'section', $routeSection );
        /*
         * Router para listados
         */
        $defaultList = array ('module' => 'default', 'controller' => 'index', 'action' => 'list', 'section' => null );
        $routeList = new Zend_Controller_Router_Route ( ':section/listar/*', $defaultList );
        $router->addRoute ( 'listados', $routeList );
        /*
         * Router para administración
         */
        $defaultAdmin = array ('module' => 'admin', 'controller' => 'index', 'action' => 'index' );
        $routeAdmin = new Zend_Controller_Router_Route ( 'admin/:controller/:action/*', $defaultAdmin );
        $router->addRoute ( 'admin', $routeAdmin );
        return $this;
    }
    /**
     * Configuración de los helpers del sistema
     *
     * @return My_Application_Bootstrap
     */
    protected function _setHelpers() {
        $prefix = 'My_Controller_Action_Helpers';
        Zend_Controller_Action_HelperBroker::addPrefix ( $prefix );
        Zend_Controller_Action_HelperBroker::addPath ( APPLICATION_PATH . '/controllers/helpers', 'My_Controller_Action_Helper' );
        return $this;
    }
    /**
     * Para usar en los hijos en vez del constructor
     */
    protected function _init() {

    }
}
Was it helpful?

Solution

Create a new directory in application folder name 'modules/default' , cut paste your 'application/controllers' inside it . Then make sure your application.ini have

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = true;

If you keep 'controllers' directory inside application folder then zf will not use modular structure hence make sure you cut and paste.

OTHER TIPS

once you have your module based structure, be sure to create a bootstrap for your module with your custom routes. for example

class Example_Bootstrap extends Zend_Application_Module_Bootstrap
    {

    public function _initModuleRoutes()
    {
        $this->bootstrap('FrontController');
        $frontController = $this->getResource('FrontController');
        $router = $frontController->getRouter();

        $route = new Zend_Controller_Router_Route(
            'modulename/:action/*',
            array(
                    'module' => 'modulename',
                    'controller' => 'modulecontroller',
                    'action' => 'index'
            )
        );
        $router->addRoute('routename', $route);

        return $router;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top