Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Directing question

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

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

    Directing question Link to this post

    Hi, I've got a site I'm working on that has pages for each region of New Zealand. I made a RegionHolder that contains Region objects (that inherit from Page).

    What I want is for the URL "/region/waikato" to display the Waikato region/page. I was thinking Director::addRules() would do it, but I'm not sure how. Should I try direct it to Region_Controller, or Region? Or something else that makes the correct Region object? Or maybe it would be best to just go to a page called "region", which has a method to fetch data from a Region object?

    Does this make sense?

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Directing question Link to this post

    if your region subclasses Page/SiteTree, then the region is automatically avaiable at /waikato (or otherwise specified in the URLSegment). The alternative way of showing it would be to create a "show"-function on your Region_Controller and link to /region/show/waikato. leaving out the action makes the whole thing more complicated: you have to override defaultAction() in your Region_Controller, which is called when no method called "waikato" is found in the class. in there, you call e.g. $this->show() - and work with $this->urlParams['Action'] to get your region-name (as specified by the default director-rule)

  • Nathan Cox
    Avatar
    Community Member
    99 Posts

    Re: Directing question Link to this post

    I think I'm just going to use /waikato for the URL and forget the /region/.

    It got me thinking, though...would SilverStripe benefit from a more complex routing system for people like me who want to do odd things with specific URLs? Or am I just missing the proper way to do it?

    I was just thinking something that would work like:

    Director::addRules(50, array(
       'region/$RegionName/$Action' => array(
    'Controller' => 'RegionController',
    'Action' => 'showMap'
    ),
    ));

    It would use the extra array to set defaults, for example if you went to /region/greater_putaruru it would run the showMap() method of the RegionController, and give it "greater_putaruru", but if you went to /region/greater_putaruru/listMembers it would override the default action and run the listMembers() method instead. Good idea, bad idea, or completely unnecessary?

    PS: code formatting in forum posts would be pretty handy too

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Directing question Link to this post

    "PS: code formatting in forum posts would be pretty handy too " - matts working on it

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Directing question Link to this post

    sounds like a good idea. btw, we usually fallback to index() if an action is missing in the called controller.

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Directing question Link to this post

    Given that index() is the default action, the added complexity doesn't seem justified.

    The current system would allow for this, provided you rename showMap() to index().

    Director::addRules(50, array(
    'region/$RegionName/$Action' => 'RegionController',
    ));

    I'm reluctant to add complexity to these deeply core modules unless there is a large value in them - otherwise we'll get a bloated system.

  • Nathan Cox
    Avatar
    Community Member
    99 Posts

    Re: Directing question Link to this post

    I guess it wouldn't let you do anything you can't do already, but it's an extra tool for customising your URLs...and if there's one thing I'm finicky about it's my URLs.

    Although if I really want it I can always just add the functionality myself.

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