Components in ZF
Moderator: General Moderators
Components in ZF
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?
<%= 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?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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:
$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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
A problem is that the ZF View was build by old-school sequential page builders. I would prefer to see something hierarchical like this:
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.
Code: Select all
$mainView = $registery->get('View');
$mainView->content = $this->view->render ( 'index/index.tpl' );
// let the response render the entire page(#10850)
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.
(#10850)
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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.
Hope this helps.
I don't disagree with you but "premature optimisation is the route of all evil" and the sooner everyone realises that the better.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.
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland