Should Components populate the view?

CakePHP is great with how much flexibility it can give you to approach a given situation. But I'm on the fence about whether components should be able to populate the view (like mini-controllers) or if they should be used only by the controller to pull out required data.

Here is two quick examples explaining the difference. The first, I use startup to gain access to the controller and use it to set some data for the view.

class NameComponent extends Object
    function startup(&$controller)
        $controller->set('name', 'My name is Jonathan.');

Okay, that was a pretty lame example but as you can see, I'm hooking into the controller and setting a new variable to be available from within the view. Now, let's take a look at the second example.

class NameComponent extends Object
    function startup(){ }
    function getName(){
        return 'My name is Jonathan.';

In this second example, I return the necessary data back via the getName function. Therefore, it'd be the responsibility of my controller to do the work of populating the view.

Pros and Cons

In the first example, you have the benefit of just dropping the component in place and not having to worry about it at the controller level. On the downside, the component does one task and that's it (unless you include controller/action detection to provide different or limited content). In other words, I could create a "top 10 articles" component but if I wanted to have the top 5 articles, as well for a different page then I either have to create an additional component or include some the controller/action detection. With components setting their own view variables, I also run the risk of variable conflicts.

In the second example, I have the benefit of being able to modularize more into a "Controller Helper" type of scenario. Do I want a top 5, or a top 10? Maybe I want just a few fields from 5 separate models? I can just set up a method on my component to do this and let my controller ferry the data over to the view.

What is your preferred approach?

Published August 24, 2006 · Updated August 24, 2006
Categorized as CakePHP
Short URL:


1 Comment · RSS feed
Steve said on January 26, 2007

I just started using CakePHP a few weeks ago, but I prefer the second method. Two reasons come to mind. The first is that it conforms to the MVC pattern better than the first method; that is, that the Controller populates the View. The second, as you mentioned, is that its easier to customize what you want the component to do for you.

Now that you have been using Cake for a while (seeing as this was posted many moons ago), which method do you prefer?

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.

Want to learn about scaling CSS for large projects?

I'm available for full and half-day workshops on scalable CSS architecture. I can provide on-site training for your team. Interested?
Get in touch.