Thread

This is an archived forum and the content is probably no longer be relevant, but is provided here for posterity.

The active forums are here.

Dealing with empty segments

April 14, 2010 6:40pm

Subscribe [2]
  • #1 / Apr 14, 2010 6:40pm

    furnak

    4 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

  • #2 / Apr 14, 2010 7:15pm

    mddd

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

  • #3 / Apr 14, 2010 8:01pm

    WanWizard's avatar

    WanWizard

    4475 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…

  • #4 / Apr 15, 2010 3:10am

    mddd

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

  • #5 / Apr 15, 2010 5:36am

    furnak

    4 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?

ExpressionEngine News

#eecms, #events, #releases