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 /

URL handling issue


Reply


4 Posts   1257 Views

Avatar
dbenton

Community Member, 22 Posts

10 March 2010 at 11:30am

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

Avatar
Willr

Forum Moderator, 5513 Posts

16 March 2010 at 11:56am

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()

Avatar
dbenton

Community Member, 22 Posts

16 March 2010 at 12:08pm

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

Avatar
Willr

Forum Moderator, 5513 Posts

16 March 2010 at 1:05pm

Edited: 16/03/2010 1:05pm

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.