Frustration with Zend Controller Action Helpers
Considering the fact that Zend Framework was designed with a preference towards configuration over convention, I’ve been unpleasantly surprised a few times with conventions that aren’t mentioned in the documentation and have caused me a few headaches. Naturally, the framework is a new product, and there are going to be oversights and surprises here and there. I think Zend is providing a really valuable toolset to the community, and I appreciate the amount of effort that has gone into producing the best 1.0 release possible. On the whole, I’ve really enjoyed using the Framework, and it’s human nature to inflate the significance of exceptions to the rule, so take this rant with a grain of salt.
When I waste an inordinate amount of time trying to use the Framework to implement something conceptually simple, it’s frustrating. When I’m going out of my way to use some of the provided design patterns to be a Good Developer, and some assumption in the code makes my app throw a completely unhelpful exception, that’s REALLY frustrating.
Case in point: I want users of my application to be able to download a dynamically generated Excel document. Easy: write the doc to a temporary file, then send some HTTP headers to the browser. Done it many times before, will probably do it many times again. So I figure this fits perfectly into the Action Helper model. From the Zend docs:
So I code up a class called FileDownloader that extends Zend_Controller_Action_Helper_Abstract and stick it in application/controllers/helpers/FileDownloader.php (my app is set up using a conventional directory structure). I call Zend_Controller_Action_HelperBroker::addPath( 'application/controllers/helpers', '') in my bootstrap and I figure I’m good to go.
As you may have guessed, wrong. I get an exception: Action Helper by name FileDownloader not found. That’s funny, I could have sworn it would be found! I change the path from relative to absolute from the document root. Nope. I change the path to the absolute path from my system root. No dice. I check that I named my helper class and file correctly. Check.
So now I’m forced to dive into the library internals to see what its looking for. At this point, I hate the Zend Framework. There is nothing quite like having to track down filesystem issues. WHY IS THIS HAPPENING?! WHAT ARE YOU LOOKING FOR??! After some digging around, it seems that my action helper classes HAVE to have a prefix. And not just any prefix: a prefix that ends with an underscore. Even though I can (supposedly!) explicitly set the prefix in the addPath() method, the Framework thinks it would be a good idea to tack on an underscore, y’know, to be safe. So, I have to rename my class, eh? It would have been nice if the exception I got mentioned that, rather than telling me it can’t find something that’s there. Something like:
Action Helper class _FileDownloader not found in file /root/application/controllers/helpers/FileDownloader.php
Tell me what you’re ACTUALLY looking for; don’t spit back my initial input, y’know?
Anyway, I know that the whole Framework is based around mirroring directory structures in the class naming convention, but if I’m going to be forced to adopt that convention, why even let me set a prefix, or why not mention it somewhere? There goes 30 minutes down the drain, and then another 40 writing this asinine blog post. Se la vie.
10 Comments Jump to comment form | comments rss | trackback uri
September 10, 2007 / 8:36 am
Gummi, what’s the name of your helper class? It needs to have some string prefix that ends with an underscore. For example, if your class is currently named ‘myControllerHelper’, you have to rename it to ‘_myControllerHelper’ or ‘Gummi_myControllerHelper’. Then you could use addPath(’/path/to/helper/files’), and it would work.
Take a look at line 116 of Zend/Controller/Action/HelperBroker.php in your framework library, and you’ll see why you need this underscore.
March 5, 2008 / 4:05 pm
[…] this guy “steve” who wrote “Frustration with Zend Controller Action Helpers,” I lost about nine hours trying to understand how Action Helpers work. View Helpers, on the […]
August 22, 2008 / 7:35 pm
That’s what brought here ,, action helper problem
thanx for your notations
January 13, 2009 / 1:49 pm
I’ve got the same problem, Im switching from CakePHP to Zend Framework for work, and I have to say Im really disappointed, it looks more like a bunch of libraries together than a ready-to-use framework so far.
ZF sucks balls !
And +1 for the really helpful exception message, thats completely retarded.
January 23, 2009 / 1:07 pm
Hi guys,
Given the date of this post, I’d imagine Steve (the poster) resolved his problem long ago, however for others coming to this page in search of a solution I thought I’d offer some input in the hopes it will help. When defining an action helper you need to define both the path and the action helper class prefix within your bootstrap.php file, like so:
Zend_Controller_Action_HelperBroker::addPath( ‘C:\apache\htdocs\website\application\My\Helper’, ‘My_Action_Helper’);
Your action helper will subsequently look like this:
class My_Action_Helper_InitializeStuff extends Zend_Controller_Action_Helper_Abstract
{
// Add methods here
}
Once in place, your action helper should work like a charm.
Hope this helps!
Jason
January 23, 2009 / 1:15 pm
Hi Jason,
Thanks for posting some code that actually works as an addendum to my rant, as it seems that this page is getting a fair amount of hits these days.
Yeah, I figured out the issue, I was just complaining that ZF 1.0 requires a prefix for action helpers without making that requirement explict in the docs. In fact, I think it was obfuscated with the argument to set a prefix in the ::addPath() method. Not sure if this is still the case with ZF 1.7, but I should find out shortly, since I’m starting a new project with it now.
June 9, 2009 / 8:17 am
[…] a friend of mine sent me this article – Frustration with Zend Controller Action Helpers – that only proves that Zend Framework, and I must say, one of the best frameworks I have […]
June 9, 2009 / 8:38 am
In my case, this probably has a different cause, but that horribly unhelpful exception is still unchanged even in ZF 1.7.3. It quickly makes me go from “indifferent, I’m using this because my company demands it” to “I hate this [insert extremely powerful expletive here] and will never use it of my own volition”.
The worst part is… it actually generates a somewhat more useful message, throws that as an exception and then ignores this, creating and throwing that useless and unhelpful “Action Helper by name xxx not found”. I can think of many nasty things to do to the developer who came up with that. None of them fit for publication on a civilised blog.
Wasted 4 hours trying to fix this and counting.
November 12, 2009 / 8:29 pm
Yeah, the feedback was horrible but i make it work in few minutes… follow me!!!
First…
Create your helper file YourHelperFileName.php and include it within the folder public_html\library\Zend\Controller\Action\Helper\ then name your class like this…
class Zend_Controller_Action_Helper_SendFile extends Zend_Controller_Action_Helper_Abstract{}
Second…
open the bootstrap.php file and write the next line inside your _initAutoload() protected function.
Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_YourHelperFileName());
Third…
Open your controller then code this line to memory it… then use it, and that’s all! Use your new helper within 5 minutes.
$this->_anyNameYouWant = $this->_helper->getHelper('YourHelperFileName');
Say what?
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
About this entry
Categories
- App Design (8)
- Blogs (6)
- Business (4)
- Code Philosophy (2)
- Javascript (1)
- Open Source (5)
- PHP (15)
- Thoughts (2)
- Tools (10)
- Usability (3)
- Wordpress (2)
- Zend Framework (9)

September 10, 2007 / 3:05 am
Just messing with same problem. Cannot load Action Controller anyhow - tried using addPath, addPrefix…