For the second time in two weeks, I find myself back in New York City for a project with Schematic. This is also the second time I’ve ever been to New York, the first time being, of course, just over a week ago. For anyone who hasn’t yet visited New York, if you find yourself flying into Newark and taking a cab into the city, it’ll cost you about $70. However, if you have the opportunity — and it’s nighttime — ask the cab driver to take you through the Lincoln Tunnel (via the NJ Turnpike) rather than the Holland Tunnel. You’ll get a spectacular, panoramic view of the lit-up city from uptown to downtown.
Also, special thanks to Chris Shiflett, who suggested I eat at The Playwright Tavern Act II at the corner of 46th St. and 8th Ave (the location on their website is for “Act I”). They have Murphy’s Stout on tap, and their traditional Irish breakfast is excellent!
After dinner, I walked down 8th Ave. to Times Square, and, along the way, found that my hotel is right next to where Avenue Q is playing. I hope to make it to Thursday’s show, and I’ll surely have to bring my wife next time I’m here so we can go see Chicago.
Now, enough about New York …
Just as views can be automated — that is, you don’t have to instantiate a Zend_View object; the controller does it all for you when you call
$this->render() from any controller action — you do not have to explicitly tell Zend_View where your helpers reside, as the manual suggests. Instead, place your helpers in a special “helpers” folder where the Zend_Controller_Action’s
initView() can find them.
In keeping with my previous example, your helpers would live in a “helpers” folder beneath the “views” folder:
All helpers contained in this “helpers” folder are automatically available to your views, albeit with a few caveats.
The Zend Framework manual doesn’t make it very clear how to name your helpers. It states:
The class name must, at the very minimum, end with the helper name itself, using CamelCaps. E.g., if you were writing a helper called “specialPurpose”, the class name would minimally need to be “SpecialPurpose”. You may, and should, give the class name a prefix, and it is recommended that you use ‘View_Helper’ as part of that prefix: “My_View_Helper_SpecialPurpose”.
Thus, when I first created a helper, I simply gave it the class name “TranslateText” because I was working on a translation helper. This did not work. So, I gave it another name: “My_View_Helper_TranslateText.” This also did not work. The helper was not available to my views until I specifically named it “Zend_View_Helper_TranslateText.” Then, I was able to access it from a view with
$this->translateText(). The file name, however, can be whatever you want.
If you wish to use helpers while using the automated view approach as illustrated in my last post, you must follow this nomenclature when writing helpers. If, however, you choose to explicitly instantiate Zend_View objects, then you may name your helpers however you like. Just follow the instructions in the Zend Framework manual in that case.
A few more things about helpers that the manual does make clear, but I think it’s worth making blatantly obvious: your helper class must have a method that is the same name as the helper in order for it to work; it does NOT rely on the class name and a constructor. Thus, if my helper is named Zend_View_Helper_TranslateText, then the
translateText() method is the one that will execute when I call
$this->translateText() from within a view. Also, all other methods and properties in a helper are ignored by the view, but you can include other methods and properties for your main helper method to use and call; I would recommend treating these as protected/private since you can’t access them from the view.
I hope this helps further your understanding of the Zend Framework.