Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » URL handling issue

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

Page: 1
Go to End
Author Topic: 1132 Views
  • dbenton
    Avatar
    Community Member
    22 Posts

    URL handling issue Link to this post

    I am trying to pass a parameter containing a forward slash to a controller, but it seems that the request is never making it to the controller when the forward slash is encoded. For example:

    http://www.domain.org/controller/action/this%2Fthat/

    returns a "Page not found" error page, while:

    http://www.domain.org/controller/action/something-else/

    works just fine.

    I cannot debug this with ?debug_request=1 as it does not seem to work on this URL either. Seems like a bug... Any ideas?

    Thanks,
    David

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: URL handling issue Link to this post

    I think you want to avoid any use of / in your parameters. - is fine, SS will convert - to _ for the action method I believe so /page/action-something will call action_something()

  • dbenton
    Avatar
    Community Member
    22 Posts

    Re: URL handling issue Link to this post

    Thanks, Willr.

    What I'm trying to do, basically, is pass a category name. I'd like to use the name, rather than an id, for SEO reasons. I don't get to pick the category names, so I can't necessarily exclude forward slashes.

    It seems that an encoded slash should be acceptable. I guess I will submit a ticket in Trac, but I wanted to check here first, as I figured I had just overlooked something obvious.

    Best,
    David

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: URL handling issue Link to this post

    Rather then just use what the author enters you should generate a valid URL slug and use that as the link. I'm guessing category is a dataobject? If so you can use onBeforeWrite() to add a slug field to your category.

    Heres a quick example

    <?php

    class Category extends DataObject {

    static $db = array(
    'Title' => 'Varchar(200)',
    'Slug' => 'Varchar(200)
    ...
    );
    ...

    function onBeforeWrite() {
    parent::onBeforeWrite();
    if(!$this->Slug) {
       $str = strtolower(trim($this->Title)); // use lowercase urls
       $str = preg_replace('/[^a-z0-9-]/', '-', $str); // use alphanumeric values only
       $str = preg_replace('/-+/', "-", $str);
       $this->Slug = $str;
    }
    }
    ...

    }

    Now you have a $Slug field which gives a more reliable result then using a title which is defined in the cms.

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