天天看点

Yaf通过composer整合Smarty

Yaf通过composer整合Smarty

最近在学习Yaf框架,我学习过程中呢,总是喜欢联想(瞎想),这篇文章就是在此之下的产物。

介绍:

Yaf是鸟哥用C语言写的扩展(也是一个高性能框架)。

手册http://www.laruence.com/manual

composer是 PHP 的一个依赖管理工具。

composer官网http://docs.phpcomposer.com

composer中国镜像http://www.phpcomposer.com

Smarty一个PHP的模板引擎。官网速度有点慢,就不放网址了。

正文:

运行环境:已安装yaf扩展,composer

Yaf通过composer整合Smarty

目录结构以yaf手册为准

1.在application同级目录,执行以下composer命令

composer require smarty/smarty

2.在index.php文件中引入composer自动加载(小心路径)

index.php文件内容如下:

<?php
require "../vendor/autoload.php";
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../')); /* 指向public的上一级 */

$application = new Yaf_Application( APPLICATION_PATH . "/conf/application.ini");

$application->bootstrap()->run();      

3.增加Smarty适配器

在application\library\Smarty目录(自己新建)下,新建适配器文件Adapter.php

文件内容如下:

<?php
/**
 * Created by PhpStorm.
 * User: sgyh
 * Date: 2017/5/24
 * Time: 15:42
 */
class Smarty_Adapter implements Yaf_View_Interface
{
    /**
     * Smarty object
     * @var Smarty
     */
    public $_smarty;

    /**
     * Constructor
     *
     * @param string $tmplPath
     * @param array $extraParams
     * @return void
     */
    public function __construct($tmplPath = null, $extraParams = array()) {

        $this->_smarty = new Smarty;

        if (null !== $tmplPath) {
            $this->setScriptPath($tmplPath);
        }

        foreach ($extraParams as $key => $value) {
            $this->_smarty->$key = $value;
        }
    }

    /**
     * Assign variables to the template
     *
     * Allows setting a specific key to the specified value, OR passing
     * an array of key => value pairs to set en masse.
     *
     * @see __set()
     * @param string|array $spec The assignment strategy to use (key or
     * array of key => value pairs)
     * @param mixed $value (Optional) If assigning a named variable,
     * use this as the value.
     * @return void
     */
    public function assign($spec, $value = null) {
        if (is_array($spec)) {
            $this->_smarty->assign($spec);
            return;
        }

        $this->_smarty->assign($spec, $value);
    }

    /**
     * Processes a template and returns the output.
     *
     * @param string $name The template to process.
     * @return string The output.
     */
    public function render($name,$tpl_vars=null) {
        return $this->_smarty->fetch($name);
    }

    public function setScriptPath($view_directory ){
        if (is_readable($view_directory)) {
            $this->_smarty->template_dir = $view_directory;
            return;
        }
    }
    public function getScriptPath(){
        return $this->_smarty->template_dir;
    }
    public function display($view , $tpl_vars = NULL ){
        $this->_smarty->display($view);
    }
}      

4.修改Bootstrap.php文件

Bootstrap.php内容如下:

<?php
/**
 * @name Bootstrap
 * @author sg\sgyh
 * @desc 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php
 * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */
class Bootstrap extends Yaf_Bootstrap_Abstract{

    public function _initConfig() {
      //把配置保存起来
      $arrConfig = Yaf_Application::app()->getConfig();
      Yaf_Registry::set('config', $arrConfig);
   }

   public function _initPlugin(Yaf_Dispatcher $dispatcher) {
      //注册一个插件
   }

   public function _initRoute(Yaf_Dispatcher $dispatcher) {
      //在这里注册自己的路由协议,默认使用简单路由
   }
   
   public function _initView(Yaf_Dispatcher $dispatcher){
      //在这里注册自己的view控制器,例如smarty,firekylin
        $smarty = new Smarty_Adapter(null, Yaf_Registry::get("config")->get("smarty"));
        Yaf_Dispatcher::getInstance()->setView($smarty);
   }
}      

5.在配置文件中增加Smarty配置

在conf文件夹的application.ini文件中,新增Smarty配置

;smarty.left_delimiter   = "{{"  ;设置模板提取值时候的"{"情况
;smarty.right_delimiter  = "}}"  ;
smarty.template_dir     = APPLICATION_PATH "/application/views/"
smarty.compile_dir      = APPLICATION_PATH "/application/views/templates_c/"
smarty.cache_dir        = APPLICATION_PATH "/application/views/templates_d/"
;smarty.caching = 0;      

如此,你就可以在方法里面使用Smarty了,如下:

Yaf通过composer整合Smarty

PS:之所以要用Composer,是因为使用Yaf的自动加载总是出错,而且对于Smarty里面的类会解析出错。

比如,有个Smarty_Template_Compiled类,却被解析成Smarty\Template\Compiled类,是我没有开启yaf.use_namespace的情况下。

而且网上其他方法错漏百出,只能自食其力了。