HireLee
HireLee

Reputation: 573

Bad Practice requiring file within a model?

I have built an MVC php application and was wondering, if instead of having to write out a large amount of html and set the data, could I not just have all this html (with some php) in a separate file and just require it?

For example:

$test = '<div>
         Test content
         <div>More content</div>
         </div>';
$APP->Template->setData('test', $test, FALSE);

Instead could I not just use:

$test = require("includes/content.php");
$APP->Template->setData('test', $test, FALSE);

Would this be considered as bad practice? It just seems that by requiring files, it can shorten the length of controllers.

Would be good to get anybodies advice on this matter.

Upvotes: 0

Views: 90

Answers (2)

tereško
tereško

Reputation: 58444

The files, that you aim to include, look like templates. Those should be handles by view instance, when it start to piece together a HTML document as a response. View should be requesting information from model layer (which is a layer, not a single class of object), and, based on received data, deciding what would be the appropriate form of response.

Such code has no place in controller. And even less in model layer. Model should contain only domain business logic and be completely oblivious to the presentation layer.

Upvotes: 1

JvdBerg
JvdBerg

Reputation: 21856

It depends on where you do the includes: if you do them in your Model, that would be wrong. However, in the view it would be alright.

It is a matter of separation of concerns: the model is the backbone of your application, while the view is responsible for displaying results from the model.

I do not see, how includes would shorten the length of the controller.

Upvotes: 1

Related Questions