文档章节

INTEGRATING SMARTY AND EZCOMPONENTS WITH THE ZEND FRAMEWORK

Donald Yang
 Donald Yang
发布于 2016/06/20 19:13
字数 674
阅读 5
收藏 0

Integrating Smarty and ezComponents with the Zend Framework

Here is a follow-up to the first part of the little tutorial Integrating Smarty with the Zend Framework. I want to address some of the issues in the comments of the first part and add some further information on how to setup your application to use the Travello_View_Smarty class. Along the way you learn how to integrate classes of the ezComponents.

Set up a __autoload() function

Since the Zend Framework is lacking a proper configuration class until now, I integrated the configuration class of the ezComponents. With a proper __autoload() function this is no big deal. Just download the ezComponents and add the path to the ezComponents to your include_path. Then amend your __autoload() function like this:

function __autoload($class)
{
if ('ezcBase' == $class and false == class_exists('ezcBase'))
{
require_once 'Base/src/base.php';
}
elseif ('ezc' == substr($class, 0, 3))
{
ezcBase::autoload($class);
}
else
{
Zend::loadClass($class);
}
}

Since all ezComponents classes start with "ezc" you can easily identify such classes and use them wherever you want. The first part of the if-statement makes sure that ezcBase class is autoloaded as well, so it will only be loaded when you want to use an ezComponents class. With this __autoload function you can use any ezComponent class you want to.

Use the configuration class

Using the configuration class is quite simple. I decided to use a simple .ini file for configuration and therefore I use the ezcConfigurationIniReader class. My .ini file is called 'settings.ini' and is located in the 'path/to/ini/files/' directory. The method call $reader->load() returns an ezcConfiguration object which is registered to the Zend object store.

$reader = new ezcConfigurationIniReader();
$reader->init('path/to/ini/files/', 'settings');
$config = $reader->load();
Zend::register('config', $config);

Whenever you need to access the configuration object, you can grab it from the object store and just use it.

$config = Zend::registry('config');
$dir = $config->getSetting('framework', 'controller_dir');

Just for illustration here is a sample ini file.

# Settings for Smarty
[smarty]
caching = true
cache_lifetime = 10
base_dir = /path/to/view
template_dir = /path/to/view/tpl
compile_dir = /path/to/view/cpl
config_dir = /path/to/view/cfg
cache_dir = /path/to/view/cch

# Settings for the Zend Framework
[framework]
application_dir = /path/to/application
controller_dir = /path/to/application/controller
model_dir = /path/to/application/model
view_dir = /path/to/application/view

Disconnect the dependency

In the first part of this tutorial there was an annoying dependency between the extended View class and the configuration object in the constructor of the View class. As a reminder here is the code from the first part:

public function __construct($data = array())
{
parent::__construct($data);

$config = Zend::registry('config');

$this->_smarty = new Smarty();

$this->_smarty->caching = $config->getSetting('smarty', 'caching');
$this->_smarty->cache_lifetime = $config->getSetting('smarty', 'cache_lifetime');
$this->_smarty->template_dir = $config->getSetting('smarty', 'template_dir');
$this->_smarty->compile_dir = $config->getSetting('smarty', 'compile_dir');
$this->_smarty->config_dir = $config->getSetting('smarty', 'config_dir');
$this->_smarty->cache_dir = $config->getSetting('smarty', 'cache_dir');
}

I amended the constructor to accept two parameters with settings data: one for the parent class and one for Smarty.

public function __construct($viewSettings = array(), $smartySettings = array())
{
parent::__construct($viewSettings);

$this->_smarty = new Smarty();

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

Now the instantiation of the View class will work like this.

$viewSettings = array(
'scriptPath' => array(
$config->getSetting('framework', 'view_dir'),
$config->getSetting('smarty', 'template_dir')
)
);

$smartySettings = $config->getSettingsInGroup('smarty');

$view = new Travello_View_Smarty($viewSettings, $smartySettings);
Zend::register('view', $view);

I use two different values for the 'scriptPath' setting. The first is a global template path and the second a project specific template path. For more information on cascading template paths please look at the Zend Framework Manual.

Conclusion

With these amendments the usage of the extended View class for Smarty is getting even simpler. The integrattion of the ezComponents is also very simple, so you can pick the components from both the Zend Framework and the ezComponents without any hassle.

If I find the time I will continue my little cherry-picking with integrating Propel into the Zend Framework next time.

本文转载自:http://blog.csdn.net/ffyd2000/article/details/668103

Donald Yang
粉丝 2
博文 56
码字总数 139
作品 0
成都
私信 提问
Smarty 官网推荐 ZF 整合方案

Smarty 是 PHP 官方推荐的模版引擎方案,使用预编译机制将模版编译为原生 PHP文件执行。自 Smarty 3 分支发布后,带来的是历史性的改变。相比 Smarty 2,Smarty 3 有更简洁的API、极好的性能...

ValueError
2011/02/12
3.2K
2
站群管理系统--WOC

WOC是rschome.com基于zend framework1.6框架所开发的一款开源简易站群管理系统。它允许进行网站管理、主机管理、域名管理、数据库管理、邮箱管理以及用户管理、角色管理、权限管理等一系列功...

gqy2468
2011/02/10
3.4K
0
请教如何在建站的时候选用网站主题框架?

自学PHP工作也2·3年了,接触并熟悉运用dedecms,zend framework,smarty,memcache,最近公司在做一个音乐网站,主要功能有:音乐/MV在线试听下载(普通音质免费高品质收费),类似猪八戒网...

kk982000
2013/10/25
568
7
我的 PHP Zend Framework(ZF) 框架教程连载

从6月份开始,计划对Zend Framework做一个系统的研究学习,并完成一个详细的教程。现在已经在 PHPChina和CSDN上连载。初步阶段计划了10部分内容,已经完成了4部分。 PHPChina 地址:http://...

技术小美
2017/11/23
0
0
我公司北京总部急需PHP高级工程师加盟

我公司(专做欧美项目外包)北京总部,因为项目需要,特招聘以下技能的高程工程师加盟。 如果符合要求的高手,请联系 QQ:46328029、email:13601333773@139.com,我们收到简历后,会约您来公...

黎叔
2009/12/01
424
2

没有更多内容

加载失败,请刷新页面

加载更多

MySQL8.0.17 - Multi-Valued Indexes 简述

本文主要简单介绍下8.0.17新引入的功能multi-valued index, 顾名思义,索引上对于同一个Primary key, 可以建立多个二级索引项,实际上已经对array类型的基础功能做了支持 (感觉官方未来一定...

阿里云官方博客
21分钟前
3
0
make4.1降级 make-3.81、2错误

在编译 make-3.82 的时候出现如下错误提示 glob/glob.c:xxx: undefined reference to `__alloca'` 修改 /glob/glob.c // #if !defined __alloca && !defined __GNU_LIBRARY__ # ifdef __GNUC......

Domineering
22分钟前
1
0
Rainbond集群的安装和运维的原理

本文将解读Rainbond集群的安装和运维的原理,使用户基本了解Rainbond的安装机制和运维重点,便于用户搭建大型Rainbond集群。 1.Rainbond集群节点概述 1.1 节点分类 属性 类型 说明 manage 管...

好雨云帮
34分钟前
3
0
好程序员大数据学习路线分享UDF函数

1.为什么需要UDF? 1)、因为内部函数没法满足需求。 2)、hive它本身就是一个灵活框架,允许用自定义模块功能,如可以自定义UDF、serde、输入输出等。 2.UDF是什么? UDF:user difine fun...

好程序员官方
36分钟前
3
0
Groovy中 Base64 URL和文件名安全编码

Base64 URL和文件名安全编码 Groovy支持Base64编码很长一段时间。 从Groovy 2.5.0开始,我们还可以使用Base64 URL和Filename Safe编码来使用encodeBase64Url方法对字节数组进行编码。 结果是...

白石
39分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部