Zend Framework 多模块配置 (二)
生活随笔
收集整理的這篇文章主要介紹了
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 多模塊配置 (三)
轉載于:https://www.cnblogs.com/showblog/archive/2010/07/31/1789246.html
總結
以上是生活随笔為你收集整理的Zend Framework 多模块配置 (二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: link路由器怎么升级 家里路由器如何升
- 下一篇: 战地3寒霜2引擎详解:地形与后期处理技术