-- Dan Ballance <[email protected]> wrote (on Saturday, 14 March 2009, 07:32 PM +0000): > I'm assuming this would work from the layout as easily as the view script?
A layout is just a view script, in the grand scheme of things. :) So, yes, all view helpers are accessible from the layout view script as well. > Thanks for you input,I'm learning a lot! > > On Mar 14, 2009 2:30 PM, "Matthew Weier O'Phinney" <[email protected]> > wrote: > > -- Vincent de Lau <[email protected]> wrote > (on Saturday, 14 March 2009, 12:52 PM +0100): > > > I've noticed the perfomance hit on using Zend_View_Helper_Action, but > don't > know what a better a... > > Basically, using action() is akin to using your controllers as models, > which breaks several important principles of encapsulation. > > The mantra of "use view helpers" stems from the fact that in MVC, views > are allowed to communicate with models in order to fetch content to > render. The goal, then, is to move all your business logic into models, > and then write view helpers that access these models (and, optionally, > format the information for rendering). > > As an example, consider this view helper: > > class My_View_Helper_RecentEntries extends Zend_View_Helper_Abstract > { > public $entryTemplate = '<li><a href="%s">%s</a></li>'; > > public function recentEntries() > { > $model = $this->view->entriesModel; > $recent = $model->fetchRecent(); > > $markup = '<ul class="recent-entries">'; > foreach ($recent as $entry) { > $url = $this->view->url(array( > 'controller' => 'articles', > 'action' => 'view', > 'id' => $entry->id, > ), > 'default', > true > ); > $markup .= sprintf( > $this->entryTemplate, > $url, > $this->view->escape($entry->title) > ); > } > $markup .= '</ul>'; > > return $markup; > } > } > > In this case, we're pulling from a model we've injected into the view, > and then creating an unordered list which we return. In your view > script, you then simply echo the results: > > <?php echo $this->recentEnties() ?> > > and that's it. This tactic provides better encapsulation, provides > behavior that is easily testable, and is much more performant as there > is no need to lookup the controller and dispatch it. > > Your suggestion that if performance is an issue with action(), effort > might go into improving the dispatcher or creating a lightweight version > makes sense in theory... but to do so actually exposes an important > point: modifying how the dispatch sequence works would actually > potentially break action(). > > Ironically, the method of using view helpers instead of action() makes > it *easier* to replace elements of Zend_Controller with your own > implementations, as you no longer need to worry that changing the > implementation will break the functionality. > > > > -----Original Message----- > > From: keith Pope [mailto: > [email protected]] > > Sent: Sa... > > -- > Matthew Weier O'Phinney > Software Architect | [email protected] > Zend Framework | http://framework.zend.com/ > -- Matthew Weier O'Phinney Software Architect | [email protected] Zend Framework | http://framework.zend.com/
