Yaf通過composer整合Smarty
最近在學習Yaf框架,我學習過程中呢,總是喜歡聯想(瞎想),這篇文章就是在此之下的產物。
介紹:
Yaf是鳥哥用C語言寫的擴展(也是一個高性能框架)。
composer是 PHP的一個依賴管理工具。
Smarty一個PHP的模板引擎。官網速度有點慢,就不放網址了。
正文:
運行環境:已安裝yaf擴展,composer
目錄結構以yaf手冊為准
1.在application同級目錄,執行以下composer命令
composer require smarty/smarty
2.在index.php文件中引入composer自動加載(小心路徑)
index.php文件內容如下:
require"../vendor/autoload.php";
define('APPLICATION_PATH', realpath(dirname(__FILE__) .'/../'));/*指向public的上一級*/$application=newYaf_Application(APPLICATION_PATH."/conf/application.ini");$application->bootstrap()->run();
3.增加Smarty適配器
在application\library\Smarty目錄(自己新建)下,新建適配器文件Adapter.php
文件內容如下:
* Created by PhpStorm.
* User: sgyh
* Date: 2017/5/24
* Time: 15:42
*/classSmarty_AdapterimplementsYaf_View_Interface
{/**
* Smarty object
* @varSmarty
*/public$_smarty;/**
* Constructor
*
* @paramstring $tmplPath
* @paramarray $extraParams
* @returnvoid
*/public function__construct($tmplPath=null,$extraParams=array()) {$this->_smarty=newSmarty;if(null!==$tmplPath) {$this->setScriptPath($tmplPath);
}foreach($extraParamsas$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()
* @paramstring|array $spec The assignment strategy to use (key or
* array of key => value pairs)
* @parammixed $value (Optional) If assigning a named variable,
* use this as the value.
* @returnvoid
*/public functionassign($spec,$value=null) {if(is_array($spec)) {$this->_smarty->assign($spec);return;
}$this->_smarty->assign($spec,$value);
}/**
* Processes a template and returns the output.
*
* @paramstring $name The template to process.
* @returnstring The output.
*/public functionrender($name,$tpl_vars=null) {return$this->_smarty->fetch($name);
}public functionsetScriptPath($view_directory){if(is_readable($view_directory)) {$this->_smarty->template_dir=$view_directory;return;
}
}public functiongetScriptPath(){return$this->_smarty->template_dir;
}public functiondisplay($view,$tpl_vars=NULL){$this->_smarty->display($view);
}
}
4.修改Bootstrap.php文件
Bootstrap.php內容如下:
* @nameBootstrap
* @authorsg\sgyh
* @desc所有在Bootstrap類中,以_init開頭的方法,都會被Yaf調用,
* @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php*這些方法,都接受一個參數:Yaf_Dispatcher $dispatcher
*調用的次序,和申明的次序相同*/classBootstrapextendsYaf_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=newSmarty_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了,如下:
PS:之所以要用Composer,是因為使用Yaf的自動加載總是出錯,而且對於Smarty里面的類會解析出錯。
比如,有個Smarty_Template_Compiled類,卻被解析成Smarty\Template\Compiled類,是我沒有開啟yaf.use_namespace的情況下。
而且網上其他方法錯漏百出,只能自食其力了。