CodeIgniter Forums
Dealing with empty segments - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Dealing with empty segments (/thread-29593.html)



Dealing with empty segments - El Forum - 04-14-2010

[eluser]furnak[/eluser]
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


Dealing with empty segments - El Forum - 04-14-2010

[eluser]mddd[/eluser]
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.


Dealing with empty segments - El Forum - 04-14-2010

[eluser]WanWizard[/eluser]
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...


Dealing with empty segments - El Forum - 04-15-2010

[eluser]mddd[/eluser]
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..


Dealing with empty segments - El Forum - 04-15-2010

[eluser]furnak[/eluser]
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?