Integrating Smarty and ez Components with the Zend Framework
http://devzone.zend.com/node/view/id/156
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 eZ Components.
Since the Zend Framework is lacking a proper configuration class until now, I integrated the configuration class of the ez Components. With a proper __autoload() function this is no big deal. Just download the ez Components and add the path to the ez Components to your include_path. Then amend your __autoload() function like this:
function __autoload( $class )
{
if ( ' ezcBase ' == $class )
{
require_once ' Base/src/base.php ' ;
}
elseif ( ' ezc ' == substr ( $class , 0 , 3 ))
{
ezcBase :: autoload( $class );
}
else
{
Zend :: loadClass( $class );
}
}
Since all ez Components 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 ez Components class. With this __autoload function you can use any ez Components class you want to.
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.
$reader = new ezcConfigurationIniReader();
$reader -> init( ' path/to/ini/files/ ' , ' settings ' );
$config = $reader -> load();
Zend :: register( ' config ' , $config );
Just for illustration here is a sample ini file.
# Settings for Smarty
[smarty]
caching = true
cache_lifetime = 10
base_dir = path oview
template_dir = path oview pl
compile_dir = path oviewcpl
config_dir = path oviewcfg
cache_dir = path oviewcch
# Settings for the Zend Framework
[framework]
application_dir = path oapplication
controller_dir = path oapplicationcontroller
model_dir = path oapplicationmodel
view_dir = path oapplicationview
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.
With these amendments the usage of the extended View class for Smarty is getting even simpler. The integrattion of the ez Components is also very simple, so you can pick the components from both the Zend Framework and the ez Components without any hassle.
If I find the time I will continue my little cherry-picking with integrating Propel into the Zend Framework next time.
I just corrected the misspelling of the ez Components and deleted the superfluous "and false == class_exists('ezcBase')" part of the if-statement in my __autoload() function.
Comments
In the __autoload() function you have the folowing code:
if ('ezcBase' == $class and false == class_exists('ezcBase'))
</ pre >
but you don't need the:
< pre >
and false == class_exists('ezcBase')
</ pre >
part as __autoload() will only be called when the class doesn't
exist. This part is thus superfluous.
Also, I think ezComponents is spelled like "eZ components" ;-)
regards,
Derick
thanks for your comment. I did not know yet, that the class_exists() is not necessary in an __autoload() function. Thanks for pointing that one out. Regarding the "ez Components" you are also right. Unfortunately, I cannot edit the article myself. So in the future I promise to use the correct spelling. :-)
Thanks and Best Regards,
Ralf
finally I found the "edit your article" function. Must have been blind this morning. I have corrected the two issues you mentioned.
Best Regards,
Ralf
i'm taking 1st steps migrating into this world from "just" smarty+php, and a roll-my-own procedural 'framework' (for lack of a better description ...)
i'm using your intro to get up-n-running w/ ZF + ezComponents + Smarty + ...
although i'm following (mostly) the "what", in the midst of the 'evolution' of your example, i've gotten a bit lost on the "where".
e.g., your instructions to "amend your __autoload() function like this" ... into which file? the Controller? the 'new' class file?
rather than bothering with pedantic Q&A from a noob, if not too much of an imposition, it would be really helpful just to see a page of uncommented code with all the filenames & paths, consistent with your 'evolving' example. i.e., a "after you've read/done all this, this is what you shouldend up with" page.
possible?
in the meantime, thx! for the intro :-)
paul
I just read your comment today. I think the DevZone needs notification mails to be sent for every new comment.
First, I have a bootstrap file index.php which is the only php file in my document root. It looks like this:
// load Zend Framework base class
include_once ' Zend.php ' ;
// load setup file
Zend :: loadFile( ' settings.php ' , null , true );
// fetch config object from object store
$config = Zend :: registry( ' config ' );
$dir = $config -> getSetting( ' framework ' , ' controller_dir ' );
// instantiate controller and dispatch
$controller = Zend_Controller_Front :: getInstance();
$controller -> setControllerDirectory( $dir );
$controller -> dispatch();
?>
Most of the code should be self-explaining. The interesting part for your question is the loading of the settings.php file. This file is placed outside of the document root and manages the settings for the website. The directory of the settings.php is set up in the include_path.
Here is the settings.php file:
// define your __autoload() function
function __autoload( $class )
{
}
// create ez Components configuration object and read settings from ini file
$reader = new ezcConfigurationIniReader();
$reader -> init( dirname ( __FILE__ ) , ' settings ' );
$config = $reader -> load();
Zend :: register( ' config ' , $config );
// set up settings for Zend_View
$viewSettings = array (
' scriptPath ' => array (
$config -> getSetting( ' framework ' , ' view_dir ' ) ,
$config -> getSetting( ' smarty ' , ' template_dir ' )
)
);
// set up settings for Smarty
$smartySettings = $config -> getSettingsInGroup( ' smarty ' );
// instantiate Smarty View
$view = new Travello_View_Smarty( $viewSettings , $smartySettings );
Zend :: register( ' view ' , $view );
?>
Thats basically it, I hope this answers your questions.
Best Regards,
Ralf