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.

Customising the CMS

Working with the URL


5 Posts   2278 Views


30 June 2009 at 3:45am Community Member, 288 Posts


I am working on a site in Silverstripe where I have news pages and each news page has_one category.
When someone arrives on the news page I want to show all news items (with some kind of limit) and this is no problem.

However when I want to only get news items in a specific category the best way of approaching this in Silverstripe is not clear.
I can use regular get variables but then why use mod_rewrite at all? I tried just adding /my_category to see if controller functions get fed URL segments as function variables but this does not seem to be the case.

Is there any Silverstripe sanctioned way of using the URL for function variables or will I have to write myself?

Kindly, Marcus.


30 June 2009 at 4:17am Community Member, 288 Posts

Ok so I found this tutorial

Problem is, it doesn't work. The first example

function addToCart ($action) {
echo "my ID = ".$action["ID"];
$obj = DataObject::get("myProduct", $action["ID"]);

Gets me halfway there. I get a HTTPRequest obect back but I can't access the ID inte way described above. HTTPRequest implements array_access so it seems to imply that the variables should be accessble this way but for whatever reason, they're not.

Just to clarify, HTTPRequest seems to be populated correctly, I just can't get the variables from it. Luckily I found out that you can get the variables through Director::urlParams();

Is this the best way for getting the variables or is there something else that can be done with HTTPRequest to get the relevant variables from it.

The second example in the tutorial above doesn't work at all.

Kindly, Marcus.


30 June 2009 at 8:56am (Last edited: 30 June 2009 8:57am), Forum Moderator, 5511 Posts

Marcus, see how the blog uses it (as this does tags in the url) but the correct approach is using Director::urlParam(). By default the URLs in SS are setup (Note you can override this as you wish) (Note 2: URLSegment is just a pointer to a controller class. You could use the ClassName instead of a page.

So if you had like it will call the function tag() on the controller of whatever class 'news' is (say 'NewsHolder_Controller') and you can access cheese by going Director::urlParam('ID'); // will return cheese.

If you need an example have a look how the blog module does it


30 June 2009 at 9:09am Community Member, 288 Posts

Thanks for the clarification Willr.

If the Director is the correct way of getting URL segments then the documentation for the execution pipeline ( should definately be updated.
I checked how the HTTPRequest object implements ArrayAccess and it's used to get $_GET and $_POST variables.
From reading the article I got the impression that it was the URL segments that would be accessible in this fashion. When this didn't work I got confused at first since the HTTPRequest object does store the URL segments so I thought I'd done something wrong somewhere else.

The documentation is not that old, has there been many changes to how Silverstripe works compared with the documentation that exists?


30 June 2009 at 9:16am Forum Moderator, 5511 Posts

You can use HTTPRequest if you want. You can do something like $this->request() and that contains the whole request object but this is alot more complex then what you need.

The documentation is not that old, has there been many changes to how Silverstripe works compared with the documentation that exists

Most of the content there is relevant but yes SS has undergone quite a few updates since most of it has been written and the docs do not list every way you can implement various things.