This short tutorial assumes that you are using Zend_Framework 1.8, the latest stable release as of this post. To learn more about View Helpers, the Zend Framework Manual as an entire page for it found here.
First, you need to create a helpers folder inside: applications/views. So the structure of your directories will look something like this:
application/ ----configs/ ----controllers/ ----layouts/ ----models/ ----views/ --------/helpers (This is the directory where all your view helpers will be stored.) --------/scripts
Inside the helpers directory, create a file named BaseUrl.php. The file should contain something like this:
<?php class Zend_View_Helper_BaseUrl { public function baseUrl() { $fc = Zend_Controller_Front::getInstance(); return $fc->getBaseUrl(); } } |
Usage:
To use your view helper, just do something like this in you view/layout files.
<!-- Assuming that this is a Layout / View file --> The base URL for this application is: <?php echo $this->baseUrl() ?> <!-- More HTML code here --> |
What does your Bootstrap application.ini look like to get this working? I am trying to do this but running into an issue. What I have is in Bootstrap.php
$this->bootstrap(‘view’);
$view = $this->getResource(‘view’);
$view->doctype(‘XHTML1_STRICT’);
$view->addHelperPath(APPLICATION_PATH . ‘/layouts/helpers’);
@Declan Butler, it just worked out of the box for me after using the new version of ZF. My Bootstrap.php looks like this: http://paste2.org/p/363802 And my application.ini has nothing special: http://paste2.org/p/363804
Hey,
The problem I was having was the class name. I forgot to prefix it with Zend_View_Helper_
It was Matthew Weier O’Phinney (Zend guru) who kindly spotted the mistake.
Hi Wenbert,
For generic view helpers, like baseUrl, most likely you’ll use them in pretty much all your ZF applications. It works better for me to actually put them out of the project application directory.
I borrowed the idea from Rob Allen (author of Zend Framework in Action). My common view helper files end up in /library/MyApp/View/Helper.
You just need to register the custom helper path to view. If you think this makes sense, I’ll make a post on my blog to get to the details.
Hi Jim Li,
That makes sense. Would be great if you can share your post.
Thanks,
Wenbert
Hi!
I’m having a problem with my custom helpers. By some reason, i’m getting the following error when the helper is called for my view:
Fatal error: Uncaught exception ‘Zend_Loader_PluginLoader_Exception’ with message ‘Plugin by name ‘SpecialPurpose’ was not found in the registry; used paths: Zend_View_Helper_Navigation_: Zend/View/Helper/Navigation/ My_View_Helper_: ./views/helpers/ Zend_View_Helper_: Zend/View/Helper/;./viewshelpers/’ in C:xamppincludez195ZendLoaderPluginLoader.php:406 Stack trace: #0 C:xamppincludez195ZendViewAbstract.php(1118): Zend_Loader_PluginLoader->load(‘SpecialPurpose’)
Can you tell me why???
@Anderson, did you double check your class names for your helper?
Thank you so much!! It just worked…