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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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

Routing to an action

Go to End

2 Posts   2199 Views

Nathan Cox

Community Member, 99 Posts

19 November 2009 at 10:37am

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


Community Member, 283 Posts

19 November 2009 at 3:26pm

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'])