zend框架混合使用zend视图和smarty视图
为了自由使用视图,实践摸索出一套办法。
zend自带了php视图,后缀是phtml,还有一些常用的帮助类等,假如分模块,结构类似
.
|-- application
| |-- configs
| | `-- application.ini
| |-- modules/
| | |-- bbs
| | | |-- controllers
| | | | |-- BbsapplyController.php
| | | | |-- BbstopicController.php
| | | | `-- IndexController.php
| | | |-- models
| | | `-- views
| | | |-- helpers
| | | |-- scripts
| | | `-- templates
| | | |-- Bbsapply
| | | | |-- index.tpl
| | | | `-- view.tpl
| | | |-- Bbstopic
| | | | |-- index1.tpl
| | | | |-- index2.tpl
| | | | `-- index.tpl
| | | `-- index
| | | `-- index.tpl
| | `-- default
| | |-- controllers
| | | |-- IndexController.php
| | | |-- NewsController.php
| | |-- models
| | `-- views
| | |-- helpers
| | |-- scripts
| | | `-- index
| | | |-- index.phtml
| | | `-- index2.phtml
| | `-- templates
| | `-- news
| | |-- category.tpl
| | `-- index.tpl
| `-- templates
| |-- common1.tpl
| |-- common2.tpl
| `-- common3.tpl
|-- library
| |-- Lib1.php
| |-- Lib2.php
| |-- Dao
| | |-- Lib1.php
| | `-- Lib2.php
| |-- Smarty
| | |-- Config_File.class.php
| | |-- Smarty.class.php
| | |-- Smarty_Compiler.class.php
| | |-- Templater.php
| | |-- debug.tpl
| | |-- internals
| | | |-- core.assemble_plugin_filepath.php
| | | |-- core.assign_smarty_interface.php
| | | `-- core.write_file.php
| | `-- plugins
| | |-- block.textformat.php
| | |-- compiler.assign.php
| | |-- function.assign_debug_info.php
| | `-- shared.make_timestamp.php
| |-- Templater
| | `-- plugins
| | |-- function.pagelinks.php
| | |-- function.wysiwyg.php
| | `-- modifier.title1.php
| `-- Zend
| |-- Acl
| |-- Amf
| |-- Auth
| |-- Barcode
| |-- ......
| `-- ......
|-- script
| `-- migrate
| `-- script1.php
`-- documentroot
|-- design
| |-- demo1.html
| `-- demo2.html
|-- images
| |-- bg.png
| |-- index.png
| |-- blank.gif
| |-- captcha.png
| `-- small.png
|-- css
| |-- 1.css
| `-- 2.css
|-- js
| `-- lib
| |-- jquery-1.4.2.min.js
| `-- jquery-ui-1.8.6.custom.min.js
`-- index.php
4个根目录的父目录由版本管理同步,
application 框架程序
library 类库,需要放在php.ini的include_path里,其中含了smarty和Zend框架的类库。
script 一些脚本
documentroot apache的文档根目录
把smarty的模板根目录定位到application,主要是为了有一些smarty公用的模板有地方可放。
为方便起见,zend视图完全按照zend的要求,连后缀都不改,目录结构都不改。
zend视图默认的是"模块名/views/scripts/控制器名/动作名.phtml"
smarty模板则相应使用"模块名/views/templates/控制器名/动作名.tpl"
假设有两个模块,缺省模块和论坛的bbs模块,在目录中可以看出,缺省模块的index控制器使用了zend视图,别的统统使用smarty视图。
在documentroot/index.php里有如下片段
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
//创建前端控制器对象,
$front_controller = Zend_Controller_Front::getInstance();
$front_controller->addModuleDirectory(APPLICATION_PATH . '/modules');
//分发
$front_controller->dispatch();
如果使用zend视图,则控制器类什么都不做。
在需要使用smarty的控制器里,这样
//缺省模块的新闻控制器
class NewsController extends Zend_Controller_Action
{
function init(){
$viewRenderer = $this->getHelper('viewRenderer');
$viewRenderer->setView(new Smarty_Templater())
->setViewBasePathSpec(APPLICATION_PATH )
->setViewScriptPathSpec('modules/:module/views/templates/:controller/:action.:suffix')
->setViewSuffix('tpl')
->initView();
$this->view = $this->_helper->viewRenderer->view;
parent::init();
}
/**
* 缺省模块的新闻控制器的index行为,使用samrty模板
*/
public function indexAction()
{
}
就这样,整个世界都清净了。