Zend Framework模块结构路由

时间:2011-05-17 09:50:39

标签: zend-framework

我的系统结构如下:

application/
   configs/
     application.ini
     router.php
   layouts/
   modules/
      default/
        controllers/
        forms/
        models/
        views/
        Bootstrap.php
      test/
        controllers/
        forms/
        models/
        views/
        Bootstrap.php
    Bootstrap.php

routes.php文件:

$useDefaultRoutes = false;

$routes['index'] = new Zend_Controller_Router_Route(
                       '', 
                       array('controller' => 'index',
                       'action' => 'index', 
                       'module' => 'default'));

主要Bootstrap.php文件

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public static function setRoutes(){

        $frontController = Zend_Controller_Front::getInstance(); 
        $router = $frontController->getRouter();

        $routes = array();


        if(file_exists('../application/configs/routes.php'))
        {

            require_once "configs/routes.php";

            foreach($routes as $routeName => $routeValue){
                $router->addRoute($routeName, $routeValue);
            }

            if($useDefaultRoutes == false)
            {
                $router->removeDefaultRoutes();
            }

        }

    }

    protected function _initFrontModules() {
        $this->bootstrap('frontController');
        $front = $this->getResource('frontController');     
        $this->setRoutes();
    }

}

问题

当我输入浏览器http://address时,默认模块启动IndexController和 一切似乎都没问题。但是如果我输入http://address/test,那么我会收到错误 - 找不到页面。如果我删除routes.php文件,那么它的工作原理。那么我的路由问题是什么?

我们将不胜感激。

1 个答案:

答案 0 :(得分:2)

尝试这种新的路由方式......

bootstrap.php

中加入此内容
protected function _initAutoloadModules()
{
    $autoloader = new Zend_Application_Module_Autoloader(
        array(
            'namespace' => '',
            'basePath'  => APPLICATION_PATH . '/modules/default'
        ), 
        array(
            'namespace' => 'Admin',
            'basePath'  => APPLICATION_PATH . '/modules/test'
        )            
      );
    return $autoloader;
}

这是您的application.ini

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
includePaths.library = APPLICATION_PATH "/../library"
appnamespace = "Default"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.defaultModule = "default"
resources.frontController.defaultController = "index"
resources.frontController.defaultAction = "index"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.layout = "layout"
resources.modules = ""
resources.view[] =
resources.session.remember_me_seconds = 864000
resources.session.use_only_cookies = on
includePaths.models = APPLICATION_PATH "/models/"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

并在module/default/bootstrap.php中添加以下给定变量

protected $_moduleName="default";

并在module/test/bootstrap.php中添加以下给定变量

protected $_moduleName="test";