zend framework 多模块 多模板配置

来源:互联网 发布:java socket 语音聊天 编辑:程序博客网 时间:2024/05/22 23:25
            由于近期要使用Zend Framework框架所以这两天研究了一下。在网上找了很多相关资料 ,都未配置成功. 后来还是自己弄出来 , 和网上有点小差别。如下:

          

            目录结构如上图,新增了两个模块分别为 ca 和 demo ,每个模块下都有自己的layouts 布局,并且每个模块下都有独立的Bootstrap.php 文件,首先看下application/configs/application.ini  是如何配置的:

  1. [production]
  2. phpSettings.display_startup_errors = 1
  3. phpSettings.display_errors = 1
  4. includePaths.library = APPLICATION_PATH "/../library"
  5. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  6. bootstrap.class = "Bootstrap"

  7. resources.frontController.moduleDirectory   = APPLICATION_PATH"/modules/"
  8. resources.frontController.moduleControllerDirectoryName     = "controllers"
  9. resources.frontController.defaultModule     = "ca"
  10. resources.modules[] = 

  11. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
  12. ca.resources.layout.layoutPath = APPLICATION_PATH "/modules/ca/layouts/scripts/"
  13. demo.resources.layout.layoutPath = APPLICATION_PATH "/modules/demo/layouts/scripts/"
  14. [staging : production]


  15. [testing : production]
  16. phpSettings.display_startup_errors = 1
  17. phpSettings.display_errors = 1


  18. [development : production]
  19. phpSettings.display_startup_errors = 1
  20. phpSettings.display_errors = 1
  21. resources.frontController.params.displayExceptions = 1

第8行定义模块目录

第9行定义模块下控制器目录

第10行 定义默认模块

第 13 ,14,15 行是定义layouts 布局的,14 ,15 中的ca 和 demo 是对应模块的名称

再看下application/Bootstrap.php 文件

<?phpclass Bootstrap extends Zend_Application_Bootstrap_Bootstrap{protected function _initLayoutHelper() { $this->bootstrap('frontController'); $layout = Zend_Controller_Action_HelperBroker::addHelper( new LayoutLoader() ); }}class LayoutLoader extends Zend_Controller_Action_Helper_Abstract { public function preDispatch() { $bootstrap = $this->getActionController() ->getInvokeArg('bootstrap'); $config = $bootstrap->getOptions(); $module = $this->getRequest()->getModuleName();if (isset($config[$module]['resources']['layout']['layoutPath'])) { $layoutPath = $config[$module]['resources']['layout']['layoutPath']; $this->getActionController() ->getHelper('layout') ->setLayoutPath($layoutPath); } }}


再让我们看下application/modules/ca/Bootstrap.php文件内容

 <?php/** 注意类名 有Ca_ 前缀 */class Ca_Bootstrap extends Zend_Application_Module_Bootstrap{protected function _initAutoLoad() {$autoloader = Zend_Loader_Autoloader::getInstance ();$autoloader->suppressNotFoundWarnings ( false );       $moduleLoader = new Zend_Application_Module_Autoloader (                array (                'namespace' => 'ca',                'basePath' => APPLICATION_PATH . '\modules\ca',                'resourceTypes' => array (                                'model' => array ('path' => 'models', 'namespace' => 'Models' ),                                'dbtable' => array ('path' => 'models/DbTable',                                'namespace' => 'Model_DbTable' )                                )                )         );        return $moduleLoader;    }}
对应application/modules/demo/Bootstrap.php 文件

<?php/** 注意类名有 Demo_ 前缀  继承自 Zend_Application_Module_Bootstrap 而非 Zend_Application_Bootstra_Bootstrap */class Demo_Bootstrap extends Zend_Application_Module_Bootstrap{protected function _initAutoLoad() {$autoloader = Zend_Loader_Autoloader::getInstance ();$autoloader->suppressNotFoundWarnings ( false );       $moduleLoader = new Zend_Application_Module_Autoloader (                array (                'namespace' => 'demo', //模块名                'basePath' => APPLICATION_PATH . '\modules\demo',  //模块路径                'resourceTypes' => array (                                'model' => array ('path' => 'models', 'namespace' => 'Model' ),                                        'dbtable' => array ('path' => 'models/DbTable',                                'namespace' => 'Model_DbTable' )                                )                )         );        return $moduleLoader;    }}

如果上面的两个文件继承自Zend_Application_Bootstrap_Bootstrap ,即要是

<?php/** 注意类名有 Demo_ 前缀  */class Demo_Bootstrap extends Zend_Application_Bootstrap_Bootstrap{}

则会报Maximum function nesting level of '100' reached, aborting!  这个错误

到这里我们的配置工作已经完成了90%了,但我们还要注意下非default 模块的控制器 命名,如application/modules/demo/controller/IndexController.php 的部分内容如下:

<?php/** 注意类名有 Demo_ 前缀 */class Demo_IndexController extends Zend_Controller_Action{    public function init()    {        /* Initialize action controller here */    }    public function indexAction()    {echo 'this is a demo!';        // action body    }}

      其他控制器命名也是如此。但default 模块下则不用加此前缀,在本例中ca为default 模块,故此模块下控制器的命名则不要加模块前缀。

     

       到现在我们的配置已经完成,让我们再修改各自模块下的layout/scripts/ 目录下的文件 看看效果吧微笑

           由于这是本人对Zend Framework 初步的认识,若有不合理的地方,还希望大家斧正,同时热烈欢迎大家和我交流PHP技术问题,我的email :  dormancy.jt@gmail.com  微笑








原创粉丝点击