Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Should Model and Controller be in seperate files?

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

Page: 1
Go to End
Author Topic: 1528 Views
  • keeny
    Avatar
    Community Member
    48 Posts

    Should Model and Controller be in seperate files? Link to this post

    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....

  • UncleCheese
    Avatar
    4085 Posts

    Re: Should Model and Controller be in seperate files? Link to this post

    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 NewsArticle.ss 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.

  • keeny
    Avatar
    Community Member
    48 Posts

    Re: Should Model and Controller be in seperate files? Link to this post

    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.

  • dospuntocero
    Avatar
    Community Member
    54 Posts

    Re: Should Model and Controller be in seperate files? Link to this post

    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

    1528 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.