在Zend Framework1.8以后加入的Zend_Application,可以说 Zend_Application 和其引入的 Bootstrap 及 Resource 概念,大大简化了应用程序的初始化组装过程。
下面主要说下Bootstrap.php及application.ini的配置。
1.Bootstrap
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap中主要做些初始化的工作,以_init开头的function都会在加载的过程中被运行。举例如下:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initConfig()
{
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
return $config;
}
protected function _initView()
{
//Different view implementation
$extraParams = $this->getOption('smarty');
$view = new Custom_View_Smarty($extraParams);
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$viewRenderer->setViewSuffix('html');
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
}
public function _initDoctrine()
{
require_once 'Doctrine.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->pushAutoloader(array('Doctrine', 'autoload'));
$doctrineConfig = $this->getOption('doctrine');
$connectionString = $this->getOption('resources');
$dsn = new PDO('mysql:dbname=' . $connectionString['db']['params']['dbname'] .
';host=' . $connectionString['db']['params']['host'],
$connectionString['db']['params']['username'],
$connectionString['db']['params']['password']);
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(
Doctrine::ATTR_MODEL_LOADING,
Doctrine::MODEL_LOADING_CONSERVATIVE);
// Add models and generated base classes to Doctrine autoloader
Doctrine::loadModels($doctrineConfig['models_path']);
$manager->openConnection($dsn);
return $manager;
}
}
主要是做一些初始化的工作。
2、application.ini
application.ini主要是配置Zend_Application_Resource的参数。
Zend_Application_Resource是 Zend Framework 针对 php 这种 web 开发语言的特性而加入的。它所阐述的思想是:按需加载 (Loaded On Demand) 。因为 php 每次解析都是资源循环的完整过程,这使得如何将每次php解析的代码量减至最低,就成了优化php应用程序的重要一环,也是众多框架在开发过程中的重点问题之一。
目前 Zend Framework 1.8 提供的默认资源总共10个:
1. Zend_Application_Resource_Db
2. Zend_Application_Resource_Frontcontroller
3. Zend_Application_Resource_Layout
4. Zend_Application_Resource_Locale
5. Zend_Application_Resource_Modules
6. Zend_Application_Resource_Navigation
7. Zend_Application_Resource_Router
8. Zend_Application_Resource_Session
9. Zend_Application_Resource_Translate
10. Zend_Application_Resource_View
在application.ini中配置的参数,会自动加载到resource中,自动完成初始化的过程。我的配置如下:
[production]
includePaths.library = APPLICATION_PATH "/../libs"
appnamespace = "Application"
;=========== 类自动加载的前缀
autoloadernamespaces.0 = "Zend_"
autoloadernamespaces.1 = "Custom_"
;=========== php ini配置
phpsettings.date.timezone = "Asia/Shanghai"
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
phpsettings.error_reporting = 8191
;=========== bootstrap类的路径及类名
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
;=========== front controller配置
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules/"
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.defaultModule = "default"
resources.frontController.defaultControllerName = "index"
resources.frontController.defaultAction = "index"
resources.frontController.baseUrl = "/www"
resources.frontController.noErrorHandler = 1
resources.frontController.throwExceptions = 1
;============网站模块(不同模块对应不同layout,与不同的ViewHelper)
resources.view.params.default.basePath = APPLICATION_PATH "/modules/default/views/"
;resources.view.params.default.helperPath = "Custom/View/Helper/Default/"
;resources.view.params.default.layout = "default"
;resources.view.params.default.layoutPath = APPLICATION_PATH "/modules/default/views/layouts"
;=========== 数据库配置
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "test"
resources.db.params.password = "123456"
resources.db.params.dbname = "test"
resources.db.params.prefix = "tt_"
resources.db.isDefaultTableAdapter = true
resources.db.params.driver_options.1002 = "SET NAMES UTF8;"
;============smarty
smarty.left_delimiter = "<{"
smarty.right_delimiter = "}>"
smarty.caching = 0
;============网站生成Cache配置
cache.type = file
cache.dir = APPLICATION_PATH "/cache/"
cache.pagedir = APPLICATION_PATH "/cache/page/"
cache.lifetime = 7200
cache.automatic_serialization = TRUE
;=================== doctrine ===========
doctrine.data_fixtures_path = APPLICATION_PATH "/doctrine/data/fixtures"
doctrine.models_path = APPLICATION_PATH "/models"
doctrine.migrations_path = APPLICATION_PATH "/doctrine/migrations"
doctrine.sql_path = APPLICATION_PATH "/doctrine/data/sql"
doctrine.yaml_schema_path = APPLICATION_PATH "/doctrine/schema"