Jump to:

22977 Posts in 11806 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] Function in Controller Vs Data object

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 310 Views
  • KungK
    Avatar
    Community Member
    14 Posts

    [SOLVED] Function in Controller Vs Data object Link to this post

    I'm running through the 3.0 tutorials (http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site). I'm supposed to create a function in the HomePage.php controller called LatestNews. According to the tutorial the rule of thumb is the following:

    "The controller for a page is only created when page is actually visited, while the data object is available when the page is referenced in other pages, e.g. by page controls. A good rule of thumb is to put all functions specific to the page currently being viewed in the controller; only if a function needs to be used in another page should you put it in the data object."

    My problem is that when I put the function in the controller it is exposed for everyone at the url http://localhost/home/LatestNews and I get an error when navigating to it. When I put it in the data object it's not exposed and I get a page not found.

    I've tried with private and protected without any success. Is there anyway to not expose a controller method but still use it in the templates?

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: [SOLVED] Function in Controller Vs Data object Link to this post

    You can define the methods callable from a URL by having a static $allowed_actions array in your controller that lists them all. If you have no methods you want callable, you can use static $allowed_actions = array('index');

    It appears that $allowed_actions is only mentioned in changelogs, rather than actually being documented anywhere.

  • KungK
    Avatar
    Community Member
    14 Posts

    Re: [SOLVED] Function in Controller Vs Data object Link to this post

    Exactly what I was looking for! Thanks a lot!

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