Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Working with the URL

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

Page: 1
Go to End
Author Topic: 2162 Views
  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Working with the URL Link to this post

    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.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Working with the URL Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Working with the URL Link to this post

    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

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Working with the URL Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Working with the URL Link to this post

    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.

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