PHP學習資料
http://hi.baidu.com/renxian/blog
http://www.zendchina.net
posted @ 2011-01-26 16:13 Jake.Xu 閱讀(10) 評論(0) 編輯Bootstrap
Bootstrap,顧名思義就是系鞋帶,指出發之前做的準備工作。自從Zend Framework 1.8 以後,Zend Framework 出了Zend_tool 和Zend_Application,因此Bootstrap有了比較大的調整,基本來講,只需要在index.php中調用Zend_Application,就可以直接免去過去設定config檔案,DB,view等。如果需要對這些做特別設定,可以在Bootstrap中擴充Zend_Application_Bootstrap_Bootstrap,用_init*資源函數來設定,BootStrap將依順序逐個調用這些_init*資源函數,然後執行dispatch。需要注意的有幾點:
1. 在initView的時候盡量不要標註Response屬性,這樣這個Bootstrap不會在layout之前執行個體化Response;
2. 可以判斷ajax的request頭,用以去除layout, ViewRender,errorHandler和Exception的輸出,這樣就免除在ajax頁面裡面單獨進行設定了。
3. ZF1.8之後可以通過設定driver option的方式對ZF的DB charset進行lazy load方式的設定了。
---------------------------------------------------------------------------------------------------------------
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected $_config;
protected $_cache;
protected $_requesttype;
public function run()
{
$frontController = Zend_Controller_Front::getInstance();
$frontController->dispatch();
}
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH,
));
return $autoloader;
}
protected function _initConfig()
{
// config
$this->_config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/config.ini', APPLICATION_ENV);
Zend_Registry::set('config', $this->_config);
Zend_Registry::set('env', APPLICATION_ENV);
//Load Application Level configure
$appconf = new Zend_Config_Ini(APPLICATION_PATH . '/configs/app.conf', APPLICATION_ENV);
Zend_Registry::set('appconf', $appconf);
if (1 == (int)$this->_config->debug->showpageinfo){
Zend_Registry::set('PageStartTime', microtime(true));
}
}
protected function _initDB()
{
if($this->_config->db) {
$db = Zend_Db::factory($this->_config->db);
if($this->_config->cache->tablemeta && isset($this->_cache)) { // setup Metacache to speed up
Zend_Db_Table::setDefaultMetadataCache($this->_cache);
}
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Registry::set('db', $db);
}
}
protected function _initView()
{
// view and layout setup
$view = new Zend_View(array('encoding'=>'UTF-8'));
$view->addHelperPath(APPLICATION_PATH.'/default/views/helpers/');
//$viewRendered = new Zend_Controller_Action_Helper_ViewRenderer($view);
//Zend_Controller_Action_HelperBroker::addHelper($viewRendered);
Zend_Dojo::enableView($view);
$view->dojo()->setDjConfigOption('parseOnLoad', true)
->requireModule('dijit.form.FilteringSelect')
->requireModule('custom.PairedStore');
//$view->addBasePath(realpath('./templates/default/'));
//if ajax submit
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
$this->_requesttype="ajax";
}else{
$this->_requesttype="html";
Zend_Layout::startMvc(array(
'layoutPath'=>APPLICATION_PATH . '/layouts',
'layout'=>'layout'
));
}
$view->doctype('XHTML1_STRICT');
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setView($view);
}
protected function _initFrontController()
{
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory(APPLICATION_PATH .'/controllers');
$frontController->setParam('env', APPLICATION_ENV);
if ("ajax" == $this->_requesttype){
// Disable the ErrorHandler plugin
$frontController->setParam('noErrorHandler', true);
// Disable the ViewRenderer helper
$frontController->setParam('noViewRenderer', true);
}
$frontController->throwExceptions(false);
$frontController->setBaseUrl('/');
// action helpers
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/controllers/helpers');
}
protected function _initSession()
{
if (isset($this->_config->session)){
$session = $this->_config->session;
if (empty($session->save_path)){
$session->save_path = APPLICATION_PATH. "/../data/sessions/";
}
}
Zend_Session::setOptions($this->_config->session->toarray());
}
protected function _initCache()
{
// Setup Core Cache
if ('true' == $this->_config->cache->caching){
$CacheFrontendOptions = array(
'lifeTime' => $this->_config->cache->lifetime, // cache lifetime of half a minute
'caching' => $this->_config->cache->caching,
'automatic_serialization' => $this->_config->cache->automatic_serialization, // this is default anyway
'automatic_cleaning_factor'=>$this->_config->cache->automatic_cleaning_factor,
'logging' => $this->_config->cache->logging,
);
switch($this->_config->cache->backend){
case 'memcached':
$MemcachedBackendOptions = array(
'servers'=>array(array(
'host' => $this->_config->cache->memcached->host,
'port' => $this->_config->cache->memcached->port,
'persistent' => $this->_config->cache->memcached->persistent
)));
$this->_cache = Zend_Cache::factory('Core', 'Memcached', $CacheFrontendOptions, $MemcachedBackendOptions);
break;
case 'file':
default:
$FileBackendOptions = array(
'cache_dir' => APPLICATION_PATH.'/../data/cache/',
'read_control'=>true,
'read_control_type'=>'md5',
'hashed_directory_level'=>'1'
);
$this->_cache = Zend_Cache::factory('Core', 'File', $CacheFrontendOptions, $FileBackendOptions);
break;
}//End of switch
Zend_Registry::set('cache', $this->_cache);
}
// Setup Page cache
if($this->_config->cache->page->caching){
$pageCacheRegexpsConfig = array(
'cache_with_get_variables'=>true,
'cache_with_post_variables'=>true,
'cache_with_session_variables'=>true,
'cache_with_files_variables'=>true,
'cache_with_cookie_variables'=>true
);
$pageCacheFrontendOptions = array(
'lifetime' => $this->_config->cache->lifetime,
'debug_header' => $this->_config->cache->page->debug, // for debugging
'regexps' => array(
// cache the whole IndexController
'^(.+)-lc-(.*).html' => array_merge($pageCacheRegexpsConfig,array('tags'=>array('lc'))),
)
);
if (empty($FileBackendOptions)){
$FileBackendOptions = array(
'cache_dir' => APPLICATION_PATH.'/../data/cache/',
'read_control'=>true,
'read_control_type'=>'md5',
'hashed_directory_level'=>'1'
);
}
// getting a Zend_Cache_Frontend_Page object
$page_cache = Zend_Cache::factory('Page',
'File',
$pageCacheFrontendOptions,
$FileBackendOptions
);
$page_cache->start();
}
}
protected function _initTranslation()
{
// Setup Locale
Zend_Locale::setDefault('en'); //fallback locale
$locale = new Zend_Locale('auto'); //auto detection for user locale
if (isset($this->_cache)){
Zend_Locale::setCache($this->_cache); //cache locale to speed up
}
Zend_Registry::set('Zend_Locale', $locale); //set up application-wide locale
// Setup Zend Translate
// $this->_cache = Zend_Cache::factory('Page','File',$frontendOptions,$backendOptions);
// Zend_Translate::setCache($this->_cache);
$translate = new Zend_Translate('gettext','../data/locales/',NULL,array(
'scan' =>Zend_Translate::LOCALE_DIRECTORY,
'disableNotices' => true
));
Zend_Registry::set('Zend_Translate', $translate);
// Zend_Validate_Abstract::setDefaultTranslator($translate);
Zend_Form::setDefaultTranslator($translate);
}
protected function _initRoutes(){
// $router = new Zend_Controller_Router_Rewrite();
// $router->addConfig($appconf, 'routes');
}
}
?>