您的位置:首页 > 编程语言 > PHP开发

INTEGRATING SMARTY AND EZCOMPONENTS WITH THE ZEND FRAMEWORK

2016-06-20 00:00 447 查看

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: