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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

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

Controller or Model?


Go to End


2 Posts   1555 Views

Avatar
UncleCheese

Forum Moderator, 4102 Posts

10 May 2008 at 2:24am

This may sound like a really basic question, but after developing several sites with SS, I'm still unclear on which functions belong in the Model and which belong in the Controller. Both seem to work for me most of the time when I call them from the template. Is this just a question of code organization or are there really some important rules I should be following? I know with most MVC's you want to do all of your database work in the Model and any manipulation of data in the controller. Are those the general guidelines for SS, too?

Thanks.

Avatar
Sam

Administrator, 690 Posts

10 May 2008 at 12:49pm

Yeah, those are the general guidelines.

It's more of an abstract philosophy than a set of hard-and-fast rules, but one way to think about it is: put anything that might be able to be accessed through a different interface in the model.

The model contains code that defines the "business logic" of the application you're making. The controller contains code that is specific to the particular interface you're making.

For example, in version 2.3, we're going to be releasing a RESTful API that will talk to the model layer. So, if there's anything that might be usefully accessed from a RESTful API, then it should be put into the model layer.

It also helps to ask theoretical questions about your application: "if I were going to build a mobile version of the site with a different interface, which pieces of code would I want to re-use?" That is the kind of code that you should put into the model.

However, if possible, anything that is specifically tied to the particular user-interface that you're building should be put into the controller.