http://msl.mscoming.com/space-2-do-thread-id-107.html
zf 1.10.5 多模块 多引导设置 smarty接管view层模板可用视图助手
2010-06-21
QQ 407802734(原作者的)
application/configs/application.ini
配置文件如下
[production]
; Timezone
phpSettings.date.timezone = “Asia/Shanghai”
; Debug output
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
; Include Paths
includePaths.library = APPLICATION_PATH “/../library”
;NameSpace
appnamespace = “Application”
;resources
autoloaderNamespaces[] = “MyApp_”
;resources.frontController.plugins.Whatever = “MyApp_Controller_Plugin_Whatever”
; 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.modules[] =
; Views
resources.view.basePath = APPLICATION_PATH “/modules/default/views”
;resources.layout.layout = “layout”
;resources.layout.layoutPath = APPLICATION_PATH “/layouts”
resources.view[] =
; Database
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password =
resources.db.params.dbname = test
resources.db.params.driver_options.1002 = “SET NAMES UTF8″
; session
resources.session.save_path = APPLICATION_PATH “/../data/session”
resources.session.use_only_cookies = true
resources.session.remember_me_seconds = 20
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
application/Bootstrap.php
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
//加载db
//这部分如果不是有特别的需要其实也没有必要写了
protected function _initDb() {
$resources = $this->getPluginResource ( ‘db’ );
$db = $resources->getDbAdapter ();
Zend_Db_Table::setDefaultAdapter ( $db );
Zend_Registry::set ( ‘db’, $db );
}
//加载smarty插件
public function _initView() {
Zend_Controller_Front::getInstance()->registerPlugin(new MyApp_Controller_Plugin_Smarty());
}
<?php
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {
public $view;
protected function _initAutoLoad() {
$autoloader = Zend_Loader_Autoloader::getInstance ();
$autoloader->suppressNotFoundWarnings ( false );
$moduleLoader = new Zend_Application_Module_Autoloader (
array (
‘namespace’ => ‘Admin’,
‘basePath’ => APPLICATION_PATH . ‘\modules\admin’,
‘resourceTypes’ => array (
‘model’ => array (‘path’ => ‘model’, ‘namespace’ => ‘Model’ ),
‘dbtable’ => array (‘path’ => ‘models/DbTable’,
‘namespace’ => ‘Model_DbTable’ )
)
)
);
return $moduleLoader;
}
}
library/Myapp/View/Smarty.php
<?php
require_once ‘Zend/View/Interface.php’;
require_once ‘Smarty/Smarty.class.php’;
//继续视图基类并实现视图接口,这样可使用视图助手
class MyApp_View_Smarty extends Zend_View_Abstract implements Zend_View_Interface
{
protected $_path;
protected $_engine;
public function __construct($template_dir,$compile_dir){
$this->_engine=new Smarty(new Zend_View());
$this->_engine->left_delimiter = ‘<{‘;
$this->_engine->right_delimiter = ‘}>’;
$this->_engine->template_dir = $template_dir;
$this->_engine->compile_dir = $compile_dir.’template_c/’;
//转载注释:此处也可以用调用配置文件的方式实现Smarty的动态配置
}
public function getEngine(){
return $this->_engine;
}
public function __set($key,$val){
$this->_engine->assign($key,$val);
}
public function __get($key){
return $this->_engine->get_template_vars($key);
}
public function __isset($key){
return $this->_engine->get_template_vars($key) !== null;
}
public function __unset($key){
return $this->_engine->clear_assign($key);
}
public function assign($spec,$value=null){
if(is_array($spec)){
$this->_engine->assign($spec);
return;
}
$this->_engine->assign($spec,$value);
}
public function clearVars(){
$this->_engine->clear_all_assign();
}
public function render($name){
return $this->_engine->fetch(strtolower($name));
}
public function _run(){
}
}
<?php
class MyApp_Controller_Plugin_Smarty extends Zend_Controller_Plugin_Abstract
{
//dispatchLoopStartup() 在 Zend_Controller_Front 进入其分发循环(dispatch loop)前被调用
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$module = $request->module;
$controller = $request->controller;
$templete_c = APPLICATION_PATH . ‘/modules/’.$module.’/views/scripts/’;
$compile_dir = APPLICATION_PATH . ‘/modules/’.$module.’/';
$vr=new Zend_Controller_Action_Helper_ViewRenderer();
$vr->setView(new MyApp_View_Smarty($templete_c,$compile_dir));
$vr->setViewSuffix(‘phtml’);
Zend_Controller_Action_HelperBroker::addHelper($vr);
}
}
<p id=”logged-in”>姓名:
<{ $user->username }>
<a href=”
<{ php }>
echo $this->url(array(
“controller”=>’index’,
‘action’=>’logout’
)
);
<{ /php }>
“>Logout</a></p>
<{/if}>