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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

PHP reserved word as an action?


Go to End


2 Posts   1343 Views

Avatar
p0lar_bear

Community Member, 7 Posts

9 December 2015 at 5:16am

Edited: 09/12/2015 5:21am

I've written a controller that doubles as something resembling a RESTful API to handle AJAX requests. I'd like to have this controller have a "list" action to return a JSON array of data objects (e.g. a script does a GET request to "<root url>/product/list" and gets back an array of products in JSON), but I come to find that "list" is a reserved word in PHP so I can't seem to create a method with that name. Is there some sort of workaround or should I think of a different name for the action?

Avatar
Devlin

Community Member, 344 Posts

11 December 2015 at 12:05am

You can't use list as a method name. But you can use $url_handlers to map a action name to a specific method.

private static $url_handlers = array(
	'list//$ID/$OtherID' => 'items',
);
private static $allowed_actions = array(
	'items',
);

public function items() {
	return 'list';
}