zf+smarty模块化配置

整了好几个小时看了无数个手册终于弄好。。。:'(
入口文件(index.php)的代码:[code]<?php
/**
*  Create On 2010-4-24
*  Author Been
*  QQ:281443751
*  Email:binbin1129@126.com
**/
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Asia/Shanghai');
ini_set('include_path',get_include_path() .
                                                  PATH_SEPARATOR ."./lib" .
                                                  PATH_SEPARATOR ."./model");
//require_once("Zend/Loader.php");
//Zend_Loader::registerAutoload();
require('Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);

$config = new Zend_Config_Ini('./conf/setting.ini','news');
Zend_Registry::set('config',$config);
$front=Zend_Controller_Front::getInstance();
//$front->setBaseUrl($config->weburl);
//$front->registerPlugin(new Plugin_My());
//echo $front->getBaseUrl();
//模块化目录
$front->setModuleControllerDirectoryName('controllers');//模块下放控制器得目录名
$front->addModuleDirectory($config->paths->base.'/modules');
$front->setParam('useDefaultControllerAlways', true);
//$front->setRequest(new Zend_Controller_Request_Http());
//echo $front->getDefaultModule();
//非模块化目录
//$front->setControllerDirectory($config->paths->base.'/controller');

  //数据库设置---------------------------------------------------------------------
$params=array('host'                =>        $config->database->hostname,
                                 'username'        =>        $config->database->username,
                                 'password'        =>        $config->database->password,
                                 'dbname'        =>        $config->database->database);
$db = Zend_Db::factory($config->database->type,$params);
$db->query('set names '.$config->database->charset.';');
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('db',$db);

//使用插件
$front->registerPlugin(new Plugin_Smarty());

//$front->throwExceptions(true);
//分发
$front->dispatch();[/code]关键在上边那个plugin_Smarty插件。。
下面看下这个插件得代码:[code]<?php
/**
*  Create On 2010-5-3
*  Author Been
*  QQ:281443751
*  Email:binbin1129@126.com
**/
class Plugin_Smarty extends Zend_Controller_Plugin_Abstract
{
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
//echo $this->getRequest()->module;
            //$this->getResponse()->appendBody("<p>routeStartup() called</p>/n");
            
    }

    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $config=Zend_Registry::get('config');
            $vr=new Zend_Controller_Action_Helper_ViewRenderer();
            //echo $this->getRequest()->module;
            $vr->setView(new Templater($this->getRequest()->module));
                 $vr->setViewSuffix($config->view->suffix);
                 Zend_Controller_Action_HelperBroker::addHelper($vr);
            // $this->getResponse()->appendBody("<p>routeShutdown() called</p>/n");
    }

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        
            //echo $this->getRequest()->module;
            //$this->getResponse()->appendBody("<p>dispatchLoopStartup() called</p>/n");
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        //echo $this->getRequest()->module;
        //$this->getResponse()->appendBody("<p>preDispatch() called</p>/n");
    }

    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
       //echo $this->getRequest()->module;
        //$this->getResponse()->appendBody("<p>postDispatch() called</p>/n");
    }

    public function dispatchLoopShutdown()
    {
       //echo $this->getRequest()->module;
       // $this->getResponse()->appendBody("<p>dispatchLoopShutdown() called</p>/n");
    }
}
[/code]看一下Templater类得代码:[code]<?php
/*
* Created on 2010-2-18
* Author: been <binbin1129@126.com>
* qq��281443751
*/
class Templater extends Zend_View_Abstract{

         protected $_path;
         protected $_engine;


         public function __construct($moduleName){
                 $config=Zend_Registry::get('config');

                 require_once('Smarty/Smarty.class.php');
                 $this->_engine=new Smarty();
                 
                 $this->_engine->left_delimiter         =         $config->view->left_delimiter;
                $this->_engine->right_delimiter =         $config->view->right_delimiter;
                 $this->_engine->template_dir        =        $config->paths->templates."/".$moduleName;
                $this->_engine->compile_dir                =        sprintf('%s/tmp/tpl_c/%s',$config->paths->data,$moduleName);
                $this->_engine->plugins_dir                =        array($config->paths->base.'/lib/Templater/plugins','plugins');

         }

         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(){

         }


}
?>
[/code]说下原理。。
首 先让拓展Zend_View_Abstract让smarty融入到zf中,关键是smarty得模板路径不好整,因为模块化得嘛,当然模板文件也要模块 化了,所一smarty得template_dir就得动态得配置。这就需要在生成templater实例时根据当前得模块设置当前模快所调用得模板路 径。又因为当前得模块名必须要在router以后才能通过request对象获取到,所以又得写道插件里。。。。所以代码就是那么写的了。。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值