Archive for category Zend Framework
Zend Framework – Conditionally Load Javascript Files
Posted by Stuart in jquery, Snippets, Zend Framework on April 6, 2010
I needed to load javascript files on a per controller basis in a Zend Framework (ZF) based project. In the past I would have done this with some conditional logic that would load things based on a page number or name or some such. In ZF all that is needed is to stick some code in the controller’s init method, a line in the layout header and we are good to go.
Here is the code to stick in your controller:
public function init()
{
$this->view->headScript()->appendFile('/mysite/public/js/somefile.js')
->appendFile('/mysite/public/js/someother.js');
}
Here is the line you stick in the layout.phtml header:
echo $this->headScript();
That’s it! Now you can have controller specific js loaded and keep your sites code overhead under control by keeping uneeded js files out of your layout.phtml.
Interesting Article about Caching Models in Zend Framework
Posted by Stuart in Zend Framework on May 12, 2009
Interesting article over at http://www.contentwithstyle.co.uk/content/a-caching-pattern-for-models about implementing caching with Zend Framework models. In the comments there is a good point about how to refresh such a cache with the author suggesting using something like a $this->cache->clear() to clear the cache upon adding or updating content.
Zend_Tool Set-up
Posted by Stuart in unix, Zend Framework on May 7, 2009
Download Zend Framework (1.8.0 is the current version at present… change as needed):
wget http://framework.zend.com/releases/ZendFramework-1.8.0/ZendFramework-1.8.0-minimal.tar.gz
Unpack the downloaded file:
tar zxvf ~/Downloads/ZendFramework-1.8.0-minimal.tar.gz
Move into that unpacked directory:
cd ~/Downloads/ZendFramework-1.8.0-minimal
Find out where your PHP binary is:
which php
Put zf.sh and zf.php into the same dir as the PHP binary:
cp bin/zf.sh bin/zf.php /Applications/MAMP/bin/php5/bin/
Find out your include path:
php -i | grep "include_path"
Put the contents of the library folder into that location:
cp -r library/* /Applications/MAMP/bin/php5/lib/php/
Test Zend_Tool:
zf.sh show version
For ease of use, let’s alias zf.sh to just zf. First open your bash login file:
vim ~/.bash_login
Add this line:
alias zf=zf.sh
You are now set up. Next stop, actually creating a project with Zend_Tool!
Extending Zend_View to Implement a Concrete Function…
Posted by Stuart in Snippets, Zend Framework on April 29, 2009
…without using a view helper*.
In a nutshell:
- Extend Zend_View
- Put your method in this extended class
- Instantiate the class (in your bootstrap for instance)
- Assign it to the ViewRenderer
- Pass that viewrenderer to Zend_Controller_Action_HelperBroker’s addHelper method
- Use it in your view
The details:
In /library/My/Zend/ create View.php:
class My_Zend_View extends Zend_View
{
public function _($string)
{
return Zend_Registry::get('translate')->_($string);
}
}
In your bootstrap:
require_once APPLICATION_PATH.'../library/My/Zend/View.php'; $view = new My_Zend_View(); $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer(); $viewRenderer->setView($view); Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
In your view:
echo $this->_("Hello");
Some extras. I have APPLICATION_PATH defined in index.php as:
define('APPLICATION_PATH', realpath(dirname(__FILE__)).'/application/');
and I have Zend_Translate instantiated in bootstrap.php like:
$translate = new Zend_Translate('csv', APPLICATION_PATH.'../library/Mine/source-en.csv', 'en');
and lastly, don’t forget to register $translate with the registry or that echo in the view file won’t work:
$registry->translate = $translate;
*Why not use a view helper? Well, you should, actually. The only reason for this solution is that the original need was for something that would work with the underscore notation used by Poedit. Apparently implementing a method named “_” doesn’t work in a helper so this solution extends Zend_View… adding an underscore method.
Sooo… if you should use a view helper what does that look like?
In /library/My/Helper/Translate.php
class My_Helper_Translate extends Zend_View_Helper_Abstract
{
public function translate($string)
{
return Zend_Registry::get('translate')->_($string);
}
}
In your bootstrap:
$view->addHelperPath("../library/My/Helper/", "My_Helper");
In the view:
echo $this->translate("Hello");





