在Zend中添加路由器模块

时间:2012-05-09 23:42:23

标签: php zend-framework zend-controller-router

我有这段代码:

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

但是,如果我进入这个url:/ admin /它加载/application/controllers/IndexController.php上的控制器而不是/application/modules/admin/controllers/IndexController.php中的控制器

我的完整引导就是这个:

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() {

    }
}

2 个答案:

答案 0 :(得分:2)

在应用程序文件夹名称'modules / default'中创建一个新目录, cut 将“application / controllers”粘贴到其中。然后确保你的application.ini有

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

如果你将'controllers'目录保存在应用程序文件夹中,那么zf将不会使用模块化结构,因此请确保剪切和粘贴。

答案 1 :(得分:0)

一旦拥有基于模块的结构,请务必使用自定义路由为模块创建引导程序。例如

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