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.

Archive

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

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

Directing question


Reply

7 Posts   1472 Views

Avatar
Nathan Cox

8 June 2007 at 1:06pm Community Member, 99 Posts

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?

Avatar
Ingo

10 June 2007 at 1:46pm Forum Moderator, 801 Posts

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)

Avatar
Nathan Cox

11 June 2007 at 3:17pm (Last edited: 11 June 2007 3:47pm), Community Member, 99 Posts

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

Avatar
Willr

11 June 2007 at 5:21pm (Last edited: 11 June 2007 5:22pm), Forum Moderator, 5511 Posts

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

Avatar
Ingo

11 June 2007 at 5:24pm Forum Moderator, 801 Posts

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

Avatar
Sam

11 June 2007 at 8:56pm Administrator, 685 Posts

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.

Avatar
Nathan Cox

12 June 2007 at 2:37pm Community Member, 99 Posts

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.