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, Ed, biapar, Willr, Ingo, swaiba

[SOLVED] Function in Controller Vs Data object

Go to End

3 Posts   715 Views


Community Member, 14 Posts

10 August 2012 at 9:06pm

I'm running through the 3.0 tutorials ( 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?


Community Member, 473 Posts

10 August 2012 at 10:44pm

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.


Community Member, 14 Posts

10 August 2012 at 10:48pm

Exactly what I was looking for! Thanks a lot!