EllisLab text mark
Advanced Search
     
Dealing with empty segments
Posted: 14 April 2010 06:40 PM
Joined: 2010-04-14
7 posts

Hi all,

I’m having a few problems with empty segments,

say I have a URI that goes

name/john/age/23/cat//dog//

how can I have it so that using,

$data = $this->uri->uri_to_assoc();

I end up with an array looking like

name => john
age => 23
cat => []
dog => []

instead of what I’m currently getting of,

name => john
age => 23
cat => dog

Thanks in advance

 
Posted: 14 April 2010 07:15 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-10-21
643 posts

You could make a rule in your htaccess file to rewrite double slashes to single slashes. Or make a route in /config/routes.php to do that.
Routes are just regular expressions so that should be not too hard.

 
Posted: 14 April 2010 08:01 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

I don’t think the question was to get rid of the double slashes, but to be able to use a double slash as some way of generating a ‘null’ value.

The short anwer is: you can’t, because the webserver (at least Apache does) normalizes the URI.

The long answer: you might be able to work around this by playing with the config variable ‘uri_protocol’ (check the documentation). For example the REQUEST_URI seems to be passed unaltered. The URI library probably has to be modified as well, there’s probably code in there that filters out empty values…

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 15 April 2010 03:10 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2008-10-21
643 posts

I’m sorry, I read it exactly the wrong way around.

I agree with WanWizard; it’s not easy to do. My approach would be to ask yourself why you have to make urls like this.
I assume you are in control of the pages that call these urls. So then you could just leave the empty fields out of the url,
or use ‘none’ for the value (of course that would mean that you couldn’t have cat=> ‘none’ as a real value).

Or you could send the values through POST in stead of in the url. It’s what POST is for..

 
Posted: 15 April 2010 05:36 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2010-04-14
7 posts

Cheers for the replies,

I’ll look at making it so that null values are represented,

Unfortunately I now have criteria set for how my segments look which means I have to change the segment query string to remove the variable ids.

So I have to go from this

/varName1/varVal1/varName2/varVal2/

to

/varVal1/varVal2/

If the variables are empty i’ll make sure it’s a -1/0/null whatever.

The site will be taking in deep linking so people can save search paths and these can also be linked from external sites.

How I’ve got it so far is that in the controllers constructor when instantiated the POST if isset is then processed where the segmented query string is then developed, this is then used to redirect the user using “header” back to the controller but this time with the segmented vars in the URI. While people who are linking to the site are routed to the search function.

I take it this means most of the URI helpers can’t now be used for the site? Is there an easier way to do this that anyone can think of?