用 Zend_Application 实现多模块 (multi-modules) 及多模板 (multi-templates) 应用程序

http://hi.baidu.com/17ppc/blog/item/c2425c0f5591bec37bcbe1a9.html

如果您也是开源爱好者,那么转载请注名
转自robin@ 飞鸟手机软件 [www.17ppc.com]

;=========== front 和 admin 模块的 view 参数,包括 scripts 文件路径及前缀,layout 路径及名称
resources.view.params.front.basePath                            = APPLICATION_PATH "/templates/front/default/"
resources.view.params.front.helperPathPrefix                    = "Kbs_View_Helper_Front_"
resources.view.params.front.helperPath                          = "Kbs/View/Helper/Front/"
resources.view.params.front.layout                              = "frontlayout"
resources.view.params.front.layoutPath                          = APPLICATION_PATH "/templates/front/default/layout/"

resources.view.params.admin.basePath                            = APPLICATION_PATH "/templates/admin/default/"
resources.view.params.admin.helperPathPrefix                    = "Kbs_View_Helper_Admin_"
resources.view.params.admin.helperPath                          = "Kbs/View/Helper/Admin/"
resources.view.params.admin.layout                              = "adminlayout"
resources.view.params.admin.layoutPath                          = APPLICATION_PATH "/templates/admin/default/layout/"

;=========== view 的其它参数
resources.view.params.pathCss                                   = "/public/css/"
resources.view.params.pathImg                                   = "/public/img/"
resources.view.params.pathJs                                    = "/public/js/"
resources.view.params.doctype                                   = "HTML4_STRICT"
resources.view.params.charset                                   = "utf-8"

;=========== 数据库配置
resources.db.adapter                                            = "pdo_mysql"
resources.db.params.host                                        = "localhost"
resources.db.params.username                                    = "xxx"
resources.db.params.password                                    = "xxx"
resources.db.params.dbname                                      = "xxx"
resources.db.isDefaultTableAdapter                              = true
resources.db.params.driver_options.1002                         = "SET NAMES UTF8;"

;=========== 翻译配置
resources.translate.registry_key                                = "Zend_Translate"
resources.translate.adapter                                     = "array"
resources.translate.options.scan                                = "directory"
resources.translate.data.directory                              = APPLICATION_PATH "/languages/"
resources.translate.data.fileExt                                = ".php"

;=========== locale
resources.locale                                                = true


[testing : production]
phpSettings.display_startup_errors                              = 1
phpSettings.display_errors                                      = 1
phpsettings.error_reporting                                     = 8191
resources.db.params.username                                    = "xxx"
resources.db.params.password                                    = "xxx"
resources.db.params.dbname                                      = "xxx"


[development : production]
phpSettings.display_startup_errors                              = 1
phpSettings.display_errors                                      = 1
phpsettings.error_reporting                                     = 8191
resources.db.params.username                                    = "xxx"
resources.db.params.password                                    = "xxx"
resources.db.params.dbname                                      = "xxx"

 

在这里,testing 及 development 均继承自 production 环境。值得注意的是,phpsettings.* 是内置的 php 运行环境参数设定, 当然你也可以用 .htaccess 或者直接在 php.ini 中设定好。bootstrap.* 则是初始化应用程序所需的 bootstrap 类及其路径。而 resources.* 就是我们所说的资源。

 

以上仅为个人的配置,不一定适合每个应用,具体还要按自己需要修改。

 

接下来就是 index.php 入口 :

 

// 项目根目录 
defined ( 'PROJECT_ROOT' ) 
|| define ( 'PROJECT_ROOT' , 
realpath ( dirname ( dirname ( __FILE__ ) ) ) ) ; 

// 定义到 application 的路径 
defined ( 'APPLICATION_PATH' ) 
|| define ( 'APPLICATION_PATH' , 
PROJECT_ROOT . '/application' ) ; 

// 定义开发环境 
defined ( 'APPLICATION_ENV' ) 
|| define ( 'APPLICATION_ENV' , 
( getenv ( 'APPLICATION_ENV' ) ? getenv ( 'APPLICATION_ENV' ) 
: 'production' ) ) ; 

// Include paths 
set_include_path ( implode ( PATH_SEPARATOR , array ( 
PROJECT_ROOT . '/library' 
) ) ) ; 

// Zend_Application 
require_once 'Zend/Application.php' ; 

// Create application 
$application = new Zend_Application( 
APPLICATION_ENV, 
PROJECT_ROOT . '/library/Kbs/Config/Application.ini' 
) ; 

// 开发环境下打开自动加载警告 
if ( 'production' !== APPLICATION_ENV) { 
$application -> getAutoloader ( ) -> suppressNotFoundWarnings ( false ) ; 
} 

// 我们仅加载 frontController 资源 
$application -> getBootstrap ( ) -> bootstrap ( 'FrontController' ) ; 
$application -> run ( ) ;

 

需要解释的是 APPLICATION_ENV 是预设的系统环境变量,它将用于 Application.ini 中区分应用程序运行环境,这里预设3个值:development, testing, production,分别表示开发环境,测试环境及实际运行环境。

 

注意我们在最开始仅仅载入了 frontController 这个资源,这是为了将初始化资源降到最低限度。我们将把其它资源部分地交由 Kbs_Controller_Plugin_Common 插件来按需分配,而余下部分则在需要时再通过 Zend_Application_Bootstrap_Bootstrap::bootstrap($resource) 按需加载。

 

我们使用 plugin resource 而不是使用在 bootstrap 中重载资源 (e.g. Bootstrap::_initView)的方法来进行资源管理,这样的好处是我们的 Bootstrap 类将非常简洁 :

 

// Kbs/Application/Bootstrap.php 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{ 
// 我们不需要在这里重载任何资源 
}

 

现在我们需要让程序 frontController 知道模板 (template) 的位置,为此我们不得不使用动作插件 (action plugin),这个我们已经在前面 Application.ini 中定义好了 :

 

// 自定义拓展插件,名为 common

resources.FrontController.plugins.common        = "Kbs_Controller_Plugin_Common"

 

// 自定义应用程序插件 Kbs_Controller_Plugin_Common 类 
require_once ( 'Zend/Controller/Plugin/Abstract.php' ) ; 

class Kbs_Controller_Plugin_Common extends Zend_Controller_Plugin_Abstract
{ 
// route 结束时 
public function routeShutdown( Zend_Controller_Request_Abstract $request ) 
{ 
// 获取模块名,如 admin,front 等 
$module = $request -> getModuleName ( ) ; 

// bootstrap 类 
$bootstrap = Zend_Controller_Front:: getInstance ( ) -> getParam ( 'bootstrap' ) ; 

// 加载 view 
$bootstrap -> bootstrap ( 'View' ) ; 
$view = $bootstrap -> getResource ( 'View' ) ; 
$moduleParams = $view -> $module ; 

// 配置 view 
$view -> addBasePath ( $moduleParams [ 'basePath' ] ) 
-> addHelperPath ( $moduleParams [ 'helperPath' ] , 
$moduleParams [ 'helperPathPrefix' ] ) ; 

// 加载 layout 并配置 
$bootstrap -> bootstrap ( 'Layout' ) ; 
$layout = $bootstrap -> getResource ( 'Layout' ) ; 
$layout -> setLayoutPath ( $moduleParams [ 'layoutPath' ] ) 
-> setLayout ( $moduleParams [ 'layout' ] ) ; 
} 
}

 

Plugin 是我目前所能想到的最好的解决方案,因为我们将在 routeShutdown 路由结束时很方便的取得 module 模块名,从而根据预先在 Application.ini 里面设定的配置信息,来获取相应的模板信息。

 

资源 Kbs_Application_Resource_View 定义如下 :

 

// 拓展资源 (plugin resource) view 
class Kbs_Application_Resource_View extends Zend_Application_Resource_ResourceAbstract
{ 
protected $_view ; 

// 初始化 view 
public function init( ) 
{ 
if ( null === $this -> _view) { 
// 获取从 Application.ini中 的配置 
$options = $this -> getOptions ( ) ; 
$view = new Zend_View( $options ) ; 
if ( ! empty ( $options [ 'params' ] ) ) { 
foreach ( $options [ 'params' ] as $key => $value ) { 
$view -> $key = $value ; 
} 
} 

// viewRenderer 动作助手 
$viewRenderer = Zend_Controller_Action_HelperBroker:: getStaticHelper ( 
'ViewRenderer' 
) ; 

// 保存配置好的视图对象 
$viewRenderer -> setView ( $view ) ; 
$this -> _view = $view ; 
} 
return $this -> _view; 
} 
}

 

到此,我们已经基本上完成了所有工作。接下来就是完成 layout 及 scripts 了,当然这不一定是程序员做的事了,大可交给专业设计人员。

 

上例中,只要把

 

resources.view.params.front.basePath            = APPLICATION_PATH "/templates/front/default/"
resources.view.params.front.layoutPath          = APPLICATION_PATH "/templates/front/default/layout"

 

换成

 

resources.view.params.front.basePath            = APPLICATION_PATH "/templates/front/oceanStyle/"
resources.view.params.front.layoutPath          = APPLICATION_PATH "/templates/front/oceanStyle/layout"

 

就能够把模板从 default 切换成 oceanStyle 。

 

以上便是如何用 Zend_Application 配置及组装应用程序,并完成多模块及多模板的基本过程。这里我忽略了 Zend/Application/Module 及其相关内容.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值