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.

General Questions

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

[SOLVED] Function in Controller Vs Data object


Reply

3 Posts   374 Views

Avatar
KungK

10 August 2012 at 9:06pm Community Member, 14 Posts

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?

Avatar
simon_w

10 August 2012 at 10:44pm Forum Moderator, 474 Posts

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.

Avatar
KungK

10 August 2012 at 10:48pm Community Member, 14 Posts

Exactly what I was looking for! Thanks a lot!