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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

Directing question


Go to End


7 Posts   1810 Views

Avatar
Nathan Cox

Community Member, 99 Posts

8 June 2007 at 1:06pm

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

Forum Moderator, 801 Posts

10 June 2007 at 1:46pm

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

Community Member, 99 Posts

11 June 2007 at 3:17pm

Edited: 11/06/2007 3:47pm

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

Forum Moderator, 5523 Posts

11 June 2007 at 5:21pm

Edited: 11/06/2007 5:22pm

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

Avatar
Ingo

Forum Moderator, 801 Posts

11 June 2007 at 5:24pm

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

Avatar
Sam

Administrator, 690 Posts

11 June 2007 at 8:56pm

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

Community Member, 99 Posts

12 June 2007 at 2:37pm

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.