Littlest things

Yaf集成Twig模板引擎

如果你问我,你是怎么知道编写Twig.php的,在Php的官网有有Yaf设置模板引擎的教程,而Twig的配置在Twig的官网用也有说明。
在Yaf中如果想使用其他的模板引擎,需要实现Yaf\View_Interface这个接口

Twig安装

1
composer require twig/twig

Bootstrap中加载composer的autoload.php

完整的贴出来,_initLoader 是需要的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* @name Bootstrap
* @author placeless
* @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 _initLoader()
{
/**
* Yaf\Loader::import是根据路径引入
* Yaf\Loader::getInstance()->autoload 在library目录下的的路径
* 没有namespace的DB默认引入
* namespace为lib\a\ 的路径一定要写对如class Haha
*/
Yaf\Loader::import(APPLICATION_PATH . "/vendor/autoload.php");
Yaf\Loader::getInstance()->autoload('helper');
Yaf\Loader::getInstance()->registerLocalNamespace(['T']);
}
public function _initWhoops(Yaf\Dispatcher $dispatcher){
if (Yaf\Registry::get('config')->whoops->handler) {
$run = new Whoops\Run;
$handler = new \Whoops\Handler\PrettyPageHandler();
$run->pushHandler($handler);
if (Whoops\Util\Misc::isAjaxRequest()) {
$run->pushHandler(new \Whoops\Handler\JsonResponseHandler());
}
$run->register();
}
}
/**
* @param \Yaf\Dispatcher $dispatcher
* 注册一个插件
*/
public function _initPlugin(Yaf\Dispatcher $dispatcher)
{
$objSamplePlugin = new SamplePlugin();
$dispatcher->registerPlugin($objSamplePlugin);
}
public function _initDatabaseEloquent()
{
$dbconfig = Yaf\Registry::get("config")->mysql->toArray();
$capsule = new Illuminate\Database\Capsule\Manager();
$capsule->addConnection($dbconfig);
$capsule->bootEloquent();
$capsule->setAsGlobal();
}
/**
* @param \Yaf\Dispatcher $dispatcher
* 在这里注册自己的路由协议,默认使用简单路由
*/
public function _initRoute(Yaf\Dispatcher $dispatcher)
{
}
/**
* @param \Yaf\Dispatcher $dispatcher
* 在这里注册自己的view控制器,例如smarty,firekylin
*/
public function _initView(Yaf\Dispatcher $dispatcher)
{
$twig = new Twig(APPLICATION_PATH . '/application/views', Yaf\Registry::get('config')->twig->toArray());
$dispatcher->setView($twig);
}
}

Yaf配置文件中的Twig配置

完整的贴出来,找到Twig相关的就可以了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[common]
application.directory = APPLICATION_PATH "/application"
;是否使用默认的异常捕获Controller, 如果开启, 在有未捕获的异常的时候, 控制权会交给ErrorController的errorAction方法,默认为false
;application.dispatcher.catchException = true
;在出错的时候, 是否抛出异常
application.dispatcher.throwException=true
;apache 目录下的目录名 比如工程文件在apache目录下的yaf中 如果直接是就是 /
;baseUri 一定需要设置
;WebServer下的yaf工程目录,比如我的/www/yaf,如果你的www目录就是只想yaf工程目录那么就是 /
application.baseUri = '/yaf';
application.dispatcher.defaultModule=index
application.dispatcher.defaultController=index
application.dispatcher.defaultAction=index
application.view.ext='twig'
application.modules='Index,Api,A'
application.dir = 'yaf'
[whoops]
;是否是用whoops来显示错误信息
whoops.handler = true
[mysql]
mysql.driver = 'mysql'
mysql.host = 'localhost'
mysql.database = 'chat'
mysql.username = 'root'
mysql.password = ''
mysql.charset = 'utf8'
mysql.collation = 'utf8_unicode_ci'
mysql.prefix = ''
[twig]
twig.cache = APPLICATION_PATH "/storage/twig"
[product : common : mysql : twig : whoops]
twig.debug = 0
[develop : common : mysql : twig : whoops]
twig.debug = 1

实现 Yaf\View_Interface的类

在Yaf中如果想使用其他的模板引擎,需要实现Yaf\View_Interface这个接口,我们命名为Twig.php,放在library目录下,不设置命名空间,那样就可以自动导入了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
class Twig implements Yaf\View_Interface
{
private $loader;
private $twig;
private $variables = [];
private $templateDir ;
/**
* @param string $templateDir
* @param array $options
*/
public function __construct($templateDir, array $options = array())
{
$this->setScriptPath($templateDir);
$this->loader = new Twig_Loader_Filesystem($templateDir);
$this->twig = new Twig_Environment($this->loader, $options);
}
public function __isset($name)
{
return isset($this->variables[$name]);
}
public function __set($name, $value)
{
$this->variables[$name] = $value;
}
public function __get($name)
{
return $this->variables[$name];
}
public function __unset($name)
{
unset($this->variables[$name]);
}
public function getTwig()
{
}
public function assign($name, $value = null)
{
$this->variables[$name]=$value;
}
public function setScriptPath($templateDir)
{
// $this->loader->setPaths($templateDir);
$this->templateDir = $templateDir;
}
public function getScriptPath()
{
// $paths = $this->loader->getPaths();
return $this->templateDir;
}
public function render($template, $tpl_vars = null)
{
if (!isset(pathinfo($template)['extension'])) {
$template .= '.'.Yaf\Registry::get('config')->application->view->ext;
}
if (is_array($tpl_vars)) {
foreach ($tpl_vars as $key => $val) {
$this->variables[$key]=$val;
}
}
return $this->twig->load($template)->display($this->variables);
}
public function display($template, $tpl_vars = null)
{
echo $this->render($template, $tpl_vars);
}
public function clear($name=null)
{
if (empty($name)) {
unset($this->variables);
} else {
unset($this->variables[$name]);
}
}
}

Bootstrap中设置Twig

可以在上面的代码中找到_initView,切记需要把_initView放在_initLoader的后面,在yaf中bootstrap中函数的初始化顺序是按照编写的顺序。
如果我们将_initView放在_initLoader的上面,那样就无法找到composer下载下来的Twig的类。在后面给Yaf设置Laravel的ORM时,我们也会遇到相同的情况。
因为我们需要在Route结束后操作数据库,如果数据库的配置放置在Route的后面,那么我们进入Controller的action中的时候数据库没有没初始化。

1
2
3
4
5
6
public function _initView(Yaf\Dispatcher $dispatcher)
{
$twig = new Twig(APPLICATION_PATH . '/application/views', Yaf\Registry::get('config')->twig->toArray());
$dispatcher->setView($twig);
}