Jump to:

22976 Posts in 11702 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Routing to an action

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

Page: 1
Go to End
Author Topic: 1797 Views
  • Nathan Cox
    Avatar
    Community Member
    99 Posts

    Routing to an action Link to this post

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

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Routing to an action Link to this post

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

    1797 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.