Zend Framework 多模块配置 (二)
续 Zend Framework 多模块配置 (一)
4)启动bootstrap文件:
全局bootstrap文件 (repos/application/botstrap.php)
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { /** *加载一些配置参数 */ protected function _initConfiguration() { //取得配置参数 $app = $this->getApplication(); $config = $app->getOptions(); if ( APPLICATION_ENV == 'development' ) { error_reporting( E_ALL & E_STRICT ); //设定错误报告级别 if ( isset( $config[ 'phpsettings' ] ) ) { foreach ( $config[ 'phpsettings' ] as $setting => $value ) { ini_set( $setting, $value ); //设定是否报告错误 } } } } /** *自动加载任意的命名空间 *@param $autoloader 自动加载器对象 */ protected function _initAutoload() { $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->setFallbackAutoloader( true ); //开启自动加载器 return $autoloader; } /** *取得前端控制器配置 *@param $controller 前端控制器对象 */ protected function _initController() { $this->bootstrap( 'FrontController' ); $controller = $this->getResource( 'FrontController' ); $modules = $controller->getControllerDirectory(); //得到模块控制器路径 $controller->setParam( 'prefixDefaultModule', true ); //开启缺省模块的命名空间 //注册插件类 $controller->registerPlugin( new Personal_Plugin_Log( new Zend_Log() ) ) //日志插件 ->registerPlugin( new Personal_Plugin_Loader( $modules ) ); //模块插件 return $controller; } /** *取得所有Http数据 *@param $request Http数据对象 */ protected function _initRequest() { $this->bootstrap( 'FrontController' ); $front = $this->getResource( 'FrontController' ); $request = $front->getRequest(); if ( null === $front->getRequest() ) { $request = new Zend_Controller_Request_Http(); $front->setRequest( $request ); } return $request; } /** *取得配置参数并注册数据库 *@param $db 数据库对象 */ protected function _initDatabase() { $options = $this->getApplication()->getOptions(); //Zend_Debug::dump( $options ); $db = Zend_Db::factory( $options[ 'database' ][ 'adapter' ], $options[ 'database' ][ 'params' ] ); Zend_Db_Table_Abstract::setDefaultAdapter( $db ); Zend_Registry::set( 'DB', $db ); return $db; } /** *加载所有模块 */ protected function _initModules() { // 加载所有模块(必不可少) //Call to prefent ZF from loading all modules } }//end class
default模块bootstrap文件 (repos/application/modules/default/botstrap.php)
/** *模块Bootstrap程序 */ class Default_Bootstrap extends Zend_Application_Module_Bootstrap { protected $_moduleName = 'default'; //模块名称 /** *包含路径模块下models文件所在路径 */ protected function _initAutoload() { //set_include_path:包含路径 set_include_path( implode( PATH_SEPARATOR, array( //realpath:符号链接和相对路径引用转换为相应的绝对路径 realpath( APPLICATION_PATH . '/modules/' . $this->_moduleName . '/models' ), get_include_path(), ) ) ); } /** *设置站点视图变量 *@param $view 视图对象 */ protected function _initView() { $view = new Zend_View(); $view->setEncoding( 'UTF-8' ); $view->doctype( 'XHTML1_STRICT' ); $view->headMeta()->appendHttpEquiv( 'Content-Type', 'text/html;charset=utf-8' ); $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper( 'ViewRenderer' ); $viewRenderer->setView( $view ); return $view; } }//end class
6)插件文件:
控制器插件 (/repos/library/Personal/Plugin/Loader.php)
/** *控制器插件类 *@param $modulesList 模块的路径 */ class Personal_Plugin_Loader extends Zend_Controller_Plugin_Abstract { protected $_modules; public function __construct( array $modulesList ) { $this->_modules = $modulesList; } /** *在分发循环(dispatch loop)前被调用 */ public function dispatchLoopStartup( Zend_Controller_Request_Abstract $request ) { $module = $request->getModuleName(); //取得模块名 if ( !isset( $this->_modules[ $module ] ) ) { throw new Exception( "Module does not exist!" ); } $bootstrapPath = $this->_modules[ $module ]; //dirname:返回路径中的目录名称 $bootstrapFile = dirname( $bootstrapPath ) . '/Bootstrap.php'; //模块名称首字母大写(ucfirst:字符串首字母大写) $class = ucfirst( $module ) . '_Bootstrap'; $application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/modules/' . $module . '/configs/module.ini' ); if ( Zend_Loader::loadFile( 'Bootstrap.php', dirname( $bootstrapPath ) ) && class_exists( $class ) ) { //class_exists:检查类是否已定义 $bootstrap = new $class( $application ); $bootstrap->bootstrap(); } } }//end class
日志插件 (/repos/library/Personal/Plugin/Log.php)
/** *日志记录器插件类 *该插件一定要在前端控制器run()前调用 *@param $log Zend_Log 对象 */ class Personal_Plugin_Log extends Zend_Controller_Plugin_Abstract { public function __construct( $log ) { //过滤所有优先级低于ERR的消息 $log->addFilter( new Zend_Log_Filter_Priority( Zend_Log::ERR ) ); //创建一个Writer对象,并且在指定目录创建一个web.log日志文件 $logWriter = new Zend_Log_Writer_Stream( BASE_PATH . '/../data/log/application.log' ); //创建一个Formatter对象 $logFormat = '%timestamp% %priorityName% %message%' . "\n"; $logWriter->setFormatter( new Zend_Log_Formatter_Simple( $logFormat ) ); //将Writer对象添加到Log对象中 $log->addWriter( $logWriter ); //将日志对象保存到注册表中以便随时访问 Zend_Registry::set( 'log', $log ); } }//end clas
转至 Zend Framework 多模块配置 (三)