您的位置:首页 > 编程语言 > PHP开发

zend framework 多模块 多模板配置

2012-07-25 22:37 465 查看
            由于近期要使用Zend Framework框架所以这两天研究了一下。在网上找了很多相关资料 ,都未配置成功. 后来还是自己弄出来 , 和网上有点小差别。如下:

          



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

[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

resources.frontController.moduleDirectory   = APPLICATION_PATH"/modules/"
resources.frontController.moduleControllerDirectoryName     = "controllers"
resources.frontController.defaultModule     = "ca"
resources.modules[] = 

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

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

第8行定义模块目录

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

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

再看下application/Bootstrap.php 文件

<?php

class 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  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息