Page 1 of 1

Components in ZF

Posted: Thu Apr 12, 2007 9:24 pm
by bpopp
Sorry, I know this isn't a ZF forum, but there seem to be a lot of knowledgeable users of it here. Rails has this feature where you can render a controller's action from within another controller. In Rails it's like:

<%= render_component(:controller => "list/items", :action => "list", :params => { "page_id" => @page.id }) %>

This could be called from your IndexController, for example. Anyone know a good way (documented or otherwise) to do something similar in ZF?

Posted: Thu Apr 12, 2007 9:26 pm
by John Cartwright

Posted: Thu Apr 12, 2007 9:57 pm
by bpopp
Unless I'm missing something, that's not the same thing. The idea of a component is to render some other controller's action inside your existing controller. So, for example, I might want to do something like this in my IndexController:

$this->_forward('header', 'layout');
echo $this->view->render ( 'index/index.tpl' );
$this->_forward('footer', 'layout');

Obiviously this is an oversimplified example, but hopefully you get the idea. I want to render some piece out of another controller, then come back where I left off and do some stuff, and then render another piece from another controller. The "forward" method is dispatched at the end of the existing controller's method so my "header" output actually ends up getting echoed after the view renders. From the docs:
If called in preDispatch(), the currently requested action will be skipped in favor of the new one. Otherwise, after the current action is processed, the action requested in _forward() will be executed.

Posted: Fri Apr 13, 2007 1:59 am
by Christopher
A problem is that the ZF View was build by old-school sequential page builders. I would prefer to see something hierarchical like this:

Code: Select all

$mainView = $registery->get('View');
$mainView->content = $this->view->render ( 'index/index.tpl' );
// let the response render the entire page
The main issue to deal with a parent view outside the Action is that you usually want the default, but sometimes you want to replace it. So it would be best if it was not created until after the action runs. Sorting out how to do that will hopefully be covered in the ZF App thread.

Posted: Fri Apr 13, 2007 6:38 am
by Maugrim_The_Reaper
ZF probably does support subcontrolling - you might need to add a subclass to control it though. The main issue is whether it's worth it. In most cases, dispatching is an expensive task and you'd want to minimise it as much as possible. In Rails it's generally recommended to avoid components where slightly more complex partial(s) would work. I'd check if a View Helper in the framework could help - yes, you'd still need to retrieve a model for the View Helper to use in building the component it would be faster, and depending on the amount of data the view need, not a huge problem.

Posted: Fri Apr 13, 2007 1:34 pm
by Christopher
A simple solution is creating a render tree by adding some simple sense of hierarchy to the Views. Usually, like in this case, you don't really need a new Controller. It is usually Views that need to be organized. The next level of complexity are Model/View pairs. Those create simple widgets/thingies to display somewhere in the main view. Having full sub-MVC components is rarer and I am not sure whether they are worth the baggage.

Posted: Sat Apr 14, 2007 6:19 pm
by Ollie Saunders
I subscribe to keeping as much functionality outside of the controllers themselves as is humanly possible limiting them to instantiation. This way no functionality is bound to any specific action or controller and a controller. This gives you a lot more flexibility. However it can involve a far bit of effort to achieve this. This is one of the reasons why I am no longer using the Zend Framework! - more on that later.

Hope this helps.
The main issue is whether it's worth it. In most cases, dispatching is an expensive task and you'd want to minimise it as much as possible.
I don't disagree with you but "premature optimisation is the route of all evil" and the sooner everyone realises that the better.

Posted: Sat Apr 14, 2007 7:00 pm
by Maugrim_The_Reaper
I don't consider it an optimisation that's premature - it's common sense not to duplicate one of the most expensive tasks in an application.