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.

Customising the CMS /

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

Working with the URL


Go to End


5 Posts   3379 Views

Avatar
MarcusDalgren

Community Member, 288 Posts

30 June 2009 at 3:45am

Hello.

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.

Avatar
MarcusDalgren

Community Member, 288 Posts

30 June 2009 at 4:17am

Ok so I found this tutorial http://doc.silverstripe.org/doku.php?id=execution-pipeline

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

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

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.

Avatar
Willr

Forum Moderator, 5523 Posts

30 June 2009 at 8:56am

Edited: 30/06/2009 8:57am

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 www.site.com/URLSegment/Action/ID/OtherID (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 www.site.com/news/tag/cheese 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

Avatar
MarcusDalgren

Community Member, 288 Posts

30 June 2009 at 9:09am

Thanks for the clarification Willr.

If the Director is the correct way of getting URL segments then the documentation for the execution pipeline (http://doc.silverstripe.org/doku.php?id=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?

Avatar
Willr

Forum Moderator, 5523 Posts

30 June 2009 at 9:16am

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.