Zend Framework整合smarty方法

来源:互联网 发布:bt4破解软件下载 编辑:程序博客网 时间:2024/04/30 08:34

index.php入口文件:

<?php
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Asia/Shanghai');
define('WEB_ROOT', 'http://localhost/zendframework/');//为分页设置的变量,pagestyle.phtml文件要用
set_include_path('.' .PATH_SEPARATOR .'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR . get_include_path());

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();//设置Zend Framework 自动载入类文件

//配制smarty参数
include "./public/scripts/Smarty/Smarty.class.php";//引入smarty文件
$smarty = new Smarty();
$smarty -> template_dir = "./application/views/scripts/";//设置模板显示路径
$smarty -> compile_dir = "./application/views/scripts/templates_c";

$smarty -> left_delimiter = "<{";
$smarty -> right_delimiter = "}>";
$registry = Zend_Registry::getInstance();
$registry->set('smarty',$smarty);

   
//配置数据库参数,并连接数据库
$config=new Zend_Config_Ini('./application/config/config.ini',null, true);
Zend_Registry::set('config',$config);
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
$dbAdapter->query("SET NAMES {$config->general->db->config->charset}");
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);
Zend_Registry::set('dbprefix',$config->general->db->config->prefix);   
//设置控制器
$frontController =Zend_Controller_Front::getInstance();
$frontController->setBaseUrl('/zendframework')//设置基本路径
                ->setParam('noViewRenderer', true)
                ->setControllerDirectory('./application/controllers')
                ->throwExceptions(true)
                ->dispatch();

SmartyController.php路由文件
<?php
class SmartyController extends Zend_Controller_Action
{
     function init() {
        $this->registry = Zend_Registry::getInstance();
        $this->view = $this->registry['smarty'];
        $baseurl = $this->_request->getBaseUrl();
        $this->view->assign('baseurl',$baseurl);
     }
     function indexAction(){
         $title = "运行成功了。";
         $this->view->assign('title',$title);
         $this->view->display('smarty/index.phtml');
     }

}
最后就是模板文件了:
在zendframework/application/views/scripts/smarty文件夹里面建模板index.phtml:

这里是smarty模板:<{$title}>

这是简单了整合smarty.其实写成插件的形式更好,我正在整理。。。

本人也是刚刚学习Zend framework ,有很多不足之处,高手飘过就可以了。

好了,该吃饭了,插件也写好了。其实很简单,看下面吧。


index.php入口文件:
<?php
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Asia/Shanghai');
define('WEB_ROOT', 'http://localhost/zendframework/');//为分页设置的变量,pagestyle.phtml文件要用
set_include_path('.' .PATH_SEPARATOR .'./library'.PATH_SEPARATOR .'./application/models/'.PATH_SEPARATOR . get_include_path());

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();//设置Zend Framework 自动载入类文件

//Smarty的配制在Custom/Controller/Plugin/Smarty.php文件中
Custom_Controller_Plugin_Smarty::SmartyView();//smarty的配制文件

//配置数据库参数,并连接数据库
$config=new Zend_Config_Ini('./application/config/config.ini',null, true);
Zend_Registry::set('config',$config);
$dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
$dbAdapter->query("SET NAMES {$config->general->db->config->charset}");
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter',$dbAdapter);
Zend_Registry::set('dbprefix',$config->general->db->config->prefix);   
//设置控制器
$frontController =Zend_Controller_Front::getInstance();
$frontController->setBaseUrl('/zendframework')//设置基本路径
                ->setParam('noViewRenderer', true)
                ->setControllerDirectory('./application/controllers')
                ->throwExceptions(true)
                ->dispatch();
SmartyController.php路由文件和上面的一样。
在zendframework/application/views/scripts/smarty文件夹里面建模板index.phtml:和上面的一样。

最后就是在zendframework/library/Custom/Controller/Plugin中的插件文件了Smarty.php:

<?php
/**
* Zend_Controller_Plugin_Abstract
*/
//require_once    'Zend/Controller/Plugin/Abstract.php';
class Custom_Controller_Plugin_Smarty extends Zend_Controller_Plugin_Abstract
{
     public static function SmartyView(){
        //配制smarty参数
        include "./public/scripts/Smarty/Smarty.class.php";//引入smarty文件
        $smarty = new Smarty();
        $smarty -> template_dir = "./application/views/scripts/";//设置模板显示路径
        $smarty -> compile_dir = "./application/views/scripts/templates_c";
        $smarty -> left_delimiter = "<{";
        $smarty -> right_delimiter = "}>";
        $registry = Zend_Registry::getInstance();
        $registry->set('smarty',$smarty);
    }
}