Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Controller or Model?

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

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

Page: 1
Go to End
Author Topic: 1289 Views
  • UncleCheese
    Avatar
    4085 Posts

    Controller or Model? Link to this post

    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.

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Controller or Model? Link to this post

    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.

    1289 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.