存档

文章标签 ‘Zend’

也谈Zend构架与Smarty的集成配置

2010年4月11日

  Zend架构是一个成熟、强大的PHP Web MVC的开发框架,而smarty是一个灵活、强大的模板引擎。如果将两者结合起来,将会使PHP的Web开发轻松不少,且上手也快。

  对于Zend构架与Smarty的集成配置,网上也有不少文章说到,但我自己觉得都不够完整,且并不是真正、深入集成。我这里所说的方法,是彻底的集成,程序写法是Zend本身的带的view写法是一样的。

  首先说一下总体目录结构:

Zend目录结构

Zend目录结构

  其中,application目录中包括config目录、controllers目录、models目录等;cache目录用于smarty缓存;compile目录用于smarty编译目录;config目录放置smarty的配置;library目录放置Zend框架库及smarty库;public目录放置引导文件及js、css、images等公共文件;template目录放置.tpl模板文件。

  首先修改配置文件来支持smarty模板库(就是application/configs/application.ini文件),添加如下内容:

  

  smarty.class_path = "Smarty/Smarty.class.php"
  smarty.left_delimiter = "<*"
  smarty.right_delimiter = "*>"
  smarty.template_dir = APPLICATION_PATH "/../template"
  smarty.compile_dir = APPLICATION_PATH "/../compile"
  smarty.cache_dir = APPLICATION_PATH "/../cache"
  smarty.cache_lifetime = 600
  smarty.caching = 0
  smarty.config_dir = APPLICATION_PATH "/../config"

  第二,创建一个模板类文件Template,里面旋转初始smarty库的内容。这个文件里的类名我写为Zend_Templater,我把它放到library/Zend/下,这样它就会被自动加载:

<?php
class Zend_Templater extends Zend_View_Abstract
    {
        protected $_path;
        protected $_engine;
 
        public function __construct()
        {
           $config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', 'production');
           require_once $config->smarty->class_path;
 
            $this->_engine = new Smarty();
            $this->_engine->template_dir = $config->smarty->template_dir;
            $this->_engine->compile_dir = $config->smarty->compile_dir;
            $this->_engine->cache_dir = $config->smarty->cache_dir;
            $this->_engine->cache_lifetime = $config->smarty->cache_lifetime;
            $this->_engine->caching = $config->smarty->caching;
            $this->_engine->left_delimiter = $config->smarty->left_delimiter;
            $this->_engine->right_delimiter = $config->smarty->right_delimiter;
            $this->_engine->config_dir  = $config->smarty->config_dir;
        }
 
        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)
        {
            $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()
        { }
    }
?>

  其实也可以不放置到Zend目录且支持自动加载此文章,请见另一篇文章让Zend框架自己加载自定义的类
  第三,修改引导文件,完成smarty与Zend的集成。修改Zend的引导文件index.php,添加支持smarty的视图类:

  $vr = new Zend_Controller_Action_Helper_ViewRenderer();
  $vr->setView(new Zend_Templater());
  $vr->setViewSuffix('tpl');
  Zend_Controller_Action_HelperBroker::addHelper($vr);

  到这里,已经完成Zend与smarty的集成。这样在controller文件中,可以如下的方式写代码:

  $this->view->name = 'Zend与smarty的集成';

  然后在你的tpl文件中,像如下的方式访问设置的变量:

  <*if $name*>
     <*$name*>
  <*/if*>

  

  原创文章如转载,请注明:转载自张文杰的博客http://zhangwenjie.net ]
  本文链接地址:http://zhangwenjie.net/archives/355.html

Web , ,

让Zend框架自己加载自定义的类

2010年4月6日

  在用Zend框架编写Web程序的过程中,我们有时候需要让框架自动加载我们自己编写的一些工具类代码。我们都知道,Zend框架对以Zend_为前缘的类会自动加载的。知道这个原理后,对于我们自己的类让框架去自动加载,那么有两种方法:

  让我们自己的类也以Zend_为前缀,并将类文件放到library/Zend目录下

  这个方法是可行的。但一个缺点是,对于会让多个Web项目引用的类库来说,大家都向Zend目录放自定义的类文件是不合适的。对于一些多个项目都需要,即是非常共有的类文件,这样做一般是比较合适的,可以节约空间,也可以减少其它项目的开发时间。

  通过配置入口文件,让Zend框架自己加载我们的类文件

  这个方法需要对入口文件,即index.php文件做一些修改,这样框架就会自动加载我们的类。这个方法对只有本项目才会用到的类比较适用。

  首先创建一个include目录,和application目录(此目录包含controller、model等二级目录)同级,include目录用来放置我们在项目中需要的自定义类。然后修改入口文件index.php:

//此代码添加到包含library库路径的代码下面。这个代码向包含路径中添加我们的include目录路径
 
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../include'),
    get_include_path(),
)));
 
//中间的其它代码
 
//然后在$application->bootstrap()->run();代码的上面添加如下代码
 
Zend_Loader_AutoLoader::getInstance()->setFallbackAutoLoader(true);
 
$application->bootstrap() ->run();

  这样就完成了index.php文件对自定义类加载的修改。以后项目中的工具类、自定义的特殊类等等就可以放到include目录,在controller文件及model文件中就可以自动加载并使用了。

  

  原创文章如转载,请注明:转载自张文杰的博客http://zhangwenjie.net ]

  本文链接地址:http://zhangwenjie.net/archives/352.html

Web , ,

在Zend框架中结合Zend_Paginator和Smarty实现分页

2010年3月30日

  在一般的Web程序中都要用到分页的功能。对于使用了Zend框架的Web程序来说,结合Zend自身的View和Zend_Paginator组件,实现分页是比较轻松的事。但在Zend中整合了Smarty模板语言后,又如何使用Zend_Paginator来实现分页呢。

  其实,我们在分析了Zend_Paginator源代码后,会发现其实实现分页也是比较容易的。下面我们具体来看一下。

  首先在Controller文件中,写如下的代码:
  

  //这里写生成$paginator的代码,我省略了.直接写输出代码
  Zend_Paginator::setDefaultScrollingStyle('Sliding'); //设置分页方式
  $this->view->pages = $paginator->getPages(); //获取关于分页的信息,又如当前是第几页,一共多少页等
  $this->view->items = $paginator->getIterator(); //获取可以迭代的具体数据

  然后在 tpl文件中如此写:

   //输出具体数据 <**>是我的smarty模板中设置的输出界定符
 
  <* if $items *>
   <*foreach name = index1 from = $items*>
      <*foreach name = index2 from = $index1*>
        <*$index2*>
      <*/foreach*>
   <*/foreach*>

  下面输出页码:

<* if $pages->pageCount*>
<div>
<!-- Previous page link -->
<* if $pages->previous *>
  <a href="/index/<* $pages->previous*>">
    < Previous
  </a> |
<*else*>
  <span>< Previous</span> |
<*/if*>
 
<!-- Numbered page links -->
<* foreach from = $pages->pagesInRange name = page*>
  <* if $page != $pages->current *>
    <a href="/index/<*$page*>">
        <*$page*>
    </a> |
  <*else*>
    <*$page*> |
  <*/if*>
<* /foreach*>
 
<!-- Next page link -->
<* if $pages->next *>
  <a href="/index/<* $pages->next*>">
    Next >;
  </a>
<*else*>
  <span>Next ></span>
<*/if*>
</div>
<*/if*>

  如此就完成了在Smarty下使用Zend_Paginator实现的效果。总结一下,在使用smarty时,controller文件中必须使用$paginator->getPages()和$paginator->getIterator()来获取分页信息及实际数据。然后在smarty模板文件中使用foreach语句输出就完成了。这个方式在我linux机器上测试成功。如果大家使用中还有什么问题,请留言。

  原创文章如转载,请注明:转载自张文杰的博客http://zhangwenjie.net ]

  本文链接地址:http://zhangwenjie.net/archives/339.html

Web , , ,