I don’t think I properly understand Zend Framework view helpers
So I have been porting a little web app I wrote in plain ol’ procedural PHP to Zend Framework. Slow going, but I’m still getting the hang of ZF and translating this crap PHP to good PHP (ie, I’ve spent as much time asking “How does ZF handle foo” as saying “Why the fuck did I do it like this in the first place”).
Anyway, I don’t think I understand view helpers. A view helper is just a class that is automagically loaded and available to your views (templates).
The rules are pretty simple: a view helper to print a ‘.’ character might be named printDot (called $this->printDot() in the template). This lives as a method called printDot in a class called Zend_View_Helper_PrintDot (assuming you use no namespace prefix) in a file called PrintDot.php.
First, I tried namespacing my view helper, which resulted in epic fail. The only thing that has worked so far is the default Zend_View_Helper_ prefix. OK, this is a toy app, we’ll worry about that later.
(Update: Just kidding, this really bothers the fuck out of me. The entire namespacing thing just isn’t working, but Zend_View_Helper_ is, so obviously something’s wrong: addHelperPath(’/path’, ‘PREFIX_’) doesn’t do shit with a class named PREFIX_helper, but omitting addHelperPath() entirely or simply passing it the /path with a class named Zend_View_Helper_helper works fine. Update Update: It is also awesome that an hour of Googling shows innumerable email/forum threads that make it as far as I have gotten… So at least I’m not the only person missing something.)
My confusion results in: won’t large applications end up with dozens and dozens of 10-line view helper classes? Is there any way to cut down on the number of helpers without relying on a single monolithic class that’s just a big case statement?
I don’t want to have to maintain a separate “library” of view helpers that my team (the Little Brains) will then fuck up all over the place. (Sorry, but the technology has to be smart, as they are … not.)
OK yeah, the working answer seems to be $this->view->addHelperPath(’blah’, ‘PREFIX_’); in the controller method, and not mucking about with a new Zend_View thingee (ie, $view = new Zend_View() and so on). I assume somewhere I missed a method call doing it the hard way? I dunno.
I guess the other answer here is extend a base class, like Zend did with their form helpers .. you still have eleventy billion helpers but at least you can centralize all the common shit.



