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

Routing to an action


Reply

2 Posts   1854 Views

Avatar
Nathan Cox

19 November 2009 at 10:37am Community Member, 99 Posts

Is there are way to use Director::addRules() to route to a particular action on a controller? For example, I want to do something like

Director::addRules(10, array(
   'confirm//$ID' => array('Controller'=>'AccountPage_Controller', 'Action' => 'confirm')
));

So if someone visits /confirm/23 it'll go to AccountPage_Controller::confirm() (and pass ID = 23 in $request).

Avatar
dalesaurus

19 November 2009 at 3:26pm Community Member, 283 Posts

You don't need to specify an action as anything coming in to the rule will go to the controller::index function.

Director::addRules(10, array(
'confirm//$ID' => 'AccountPage_Controller'
));

This will route anything from /confirm* to the AccountPage_Controller. From there you can process either by action (/confirm/me to AccountPage_Controller function me()) or by handling it in the index() function.

All values passed on the URL will be available in the array $this->urlParams. So with the above rule in place in your AccountPage_Controller:

public function index() {
if( isset($this->urlParams['ID']) and is_numeric($this->urlParams['ID'])) ) {
// call some confirmation code with $this->urlParams['ID'])
}