用 Zend_Application 实现多模块 (multi-modules) 及多模板 (multi-templates) 应用程序
2011-08-10 09:47
363 查看
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 入口 :
需要解释的是 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 类将非常简洁 :
现在我们需要让程序 frontController 知道模板 (template) 的位置,为此我们不得不使用动作插件 (action plugin),这个我们已经在前面 Application.ini 中定义好了 :
// 自定义拓展插件,名为 common
resources.FrontController.plugins.common = "Kbs_Controller_Plugin_Common"
Plugin 是我目前所能想到的最好的解决方案,因为我们将在 routeShutdown 路由结束时很方便的取得 module 模块名,从而根据预先在 Application.ini 里面设定的配置信息,来获取相应的模板信息。
资源 Kbs_Application_Resource_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 及其相关内容.
如果您也是开源爱好者,那么转载请注名
转自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 及其相关内容.
相关文章推荐
- 用 Zend_Application 实现多模块 (multi-modules) 及多模板 (multi-templates) 应用程序
- 用Zend_Application实现多模块(modules)及多模板(templates)应用程序
- 在WinForm应用程序中快速实现多语言的处理(2)--开发框架模块的整合
- Zend Framework实现Zend_View集成Smarty模板系统的方法
- MOSS通过此命令注册模板,web应用程序可以根据stp模块生成网站集
- 微软Windows SharePoint Services 3.0 应用程序模板( Templates)中文版推出了
- C++模板实现模块间参数传递及回调(一)
- Python之Fabric模块 Fabric是基于Python实现的SSH命令行工具,简化了SSH的应用程序部署及系统管理任务,它提供了系统基础的操作组件,可以实现本地或远程shell命令,包括:
- 读boost::multi_array有感,多维数组实现(非类型模板,偏特化)
- 利用Tabbed Application模板实现多视图切换
- C++模板实现模块间参数传递及回调之二
- C++模板实现模块间参数传递及回调之三
- Ubuntu中为Android系统实现内置Java应用程序测试Application Frameworks层的硬件服务
- WinForm应用程序之注册模块的设计与实现
- 4.4 single_server.c,multi_server.c,trigger_server.c:业务模块服务模板
- Adhesive框架系列文章--应用程序信息中心模块实现
- Iphone开发(八)利用Tabbed Application模板实现多视图切换
- [转]MOSS通过此命令注册模板,web应用程序可以根据stp模块生成网站集
- WinForm应用程序之注册模块的设计与实现
- nodejs使用multiparty模块实现文件上传(另附express.bodyParser()的说明)