Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Should Model and Controller be in seperate files?

Go to End

4 Posts   2374 Views


Community Member, 48 Posts

20 February 2009 at 3:57pm

Many times i've scratched my head over a problem because i've put a function in the controller rather than the model. This might sound silly - but I find having them both in the same file is very confusing.

Coming from Rails, the seperation is explicit with model files and controller files. Does anyone else think it would be much clearer to have 'mysite/code' split into 'mysite/code/controllers' 'mysite/code/models'?

Please throw in your two cents....


Forum Moderator, 4102 Posts

20 February 2009 at 5:38pm

Yeah, this confused me for a while, too. See the "Model or Controller?" post in the archive.

Functions in the controller are only available to the template being controlled by the controller. Functions in the model are available everywhere. If you've syndicated your NewsArticle objects to the home page, for instance, only the functions in the NewsArticle model will be available. But when viewing the actual page, you'll have access to its controller functions.

Just a question of scope, really. It's a little more tidy to keep template-specific functions in your controller.


Community Member, 48 Posts

20 February 2009 at 5:56pm

Hi Mr Cheese,

I do understand the scope of a MVC system. I just don't think MVC is shouted about enough for new developers coming to silverstripe. I think having a model and controller side by side in the same file confuses new silverstripe developers.

I think to encourage the separation of business logic from template-specific actions, a model should be in it's own file. As should a controller be in it's own file.


Community Member, 54 Posts

23 February 2009 at 2:32am

Keeny, actually you could separate the files without problems, but what i noticed is one of the most common problems to new people using silverstripe is that forget to put its AnyPageController extends Page_Controller at the bottom of its Classes, imagine now they will need to create an empty file only for write this...

i think is better to simplify things, and when you start to do complex stuff, simply re order as you want