Warren Benedetto
Warren Benedetto

Reputation: 2538

Passing data from MVC Controller to View in PHP

I have my own hand-rolled PHP MVC framework for some projects that I'm working on. When I first created the framework, it was in the context of building an admin CMS. Therefore, there was a very nice one-to-one relationship between model, view, and controller. You have a single row in the DB, which maps to a single model. The controller loads the model and passes it to the view to be rendered (such as into an edit form). Nice, clean, and easy.

However, now that I'm working on the front end of the site, things are getting sticky. A page isn't always a view of a single model. It might be a user directory listing with 20 users (each a User model). Furthermore, there might be metadata about the request, such as pagination (current page, total pages, number of results) and/or a search query.

My question is, what is the cleanest way to pass all this data to the view?

Some options I'm considering:

I know that technically any of the could work, but I'm unsure of the best choice, or if there's a better or more conventional choice that I'm missing.

Upvotes: 15

Views: 15813

Answers (3)

Tres
Tres

Reputation: 5674

In the one I use, it has automatically has a view property in the controller that you can access methods and properties on the view. All public properties are then accessible within the view view '$this' since the view is rendered in it's own objects context.

In the controller:

$this->view->myVar = 'test';

And in the view:

$this->myVar; // 'test'

The same goes for the layout since the are both separate instances of the same view object:

$this->layout->myVar = 'test';

And then in the layout:

$this->myVar; // 'test'

The framework used to be proprietary, but is about to be released to the general public. I'd be happy to send you some code from it if you think that'd help. Remember, the simplest answer is usually the best answer.

Upvotes: 0

Peter Bailey
Peter Bailey

Reputation: 105914

I'm going to recommend the concept of Fat Models, Skinny Controllers (or, Fat Models Thin Controllers if you prefer...)

In otherwords, your model is too strict - tying your model to represent only something like a RowDataGateway is extremely limiting.

In fact, I think good models hide the fact that you're reading the data from a database at all. Because, in reality, your data could be in text files, or from a web service, or whatever. If you treat your Model like nothing more than a glorified DBAL, you doom yourself to having tightly-coupled code in your controllers that just won't let you break away from the "data only comes from the database" way of thinking.

Upvotes: 4

Marquis Wang
Marquis Wang

Reputation: 11118

I've seen both of the first two methods implemented in popular MVC/templating frameworks.

django uses the first method, passing to the view a dictionary of variables which the view uses to fill the template.

smarty uses the second method, creating a Smarty object and assigning values to each the properties in the container.

Your third method seems to essentially be the same as the second, with minor architecture differences.

Really, I guess I haven't said anything that you haven't thought of already. Basically, these are all sounds ideas, so implement whatever you feel you are most comfortable with.

Upvotes: 0

Related Questions