A more-useful uri_to_assoc |
[eluser]Benjamin Midget[/eluser]
Because I find it cumbersome to define the segment to start when using uri_to_assoc(n), I put together this piece that works much more like get: See the code here. It extends uri because uri is always loaded. After doing this, you can use my function like a get function. It automatically creates the associative array, no matter where in the url your controller and method are, and whether it's a re-routed url or not. Here's an example: Code: // for this example, the segments are Because URI is always loaded, you can do the following, too, inside your libraries without including the CI instance: Code: $vars = MY_URI::get(); Personally, I think this functionality ought to be added to the Core.
[eluser]Colin Williams[/eluser]
I dig. Maybe not get() though. Mixing metaphors, so to speak. Maybe param()?
[eluser]xwero[/eluser]
I wonder if it wouldn't be better to alter the (slash_)segment and (slash_)rsegment methods to accept a string which is the key of the key-value pair? Code: function segment($n, $no_result = FALSE)
[eluser]CtheB[/eluser]
Excellent xwero. I think many people are searching for this. Now nobody needs the ?key=value anymore! but everybody can do key/value !!! This is for people who don't know how to implement the example of xwero: Make a file called MY_URI.php and place it in your application/libraries folder with the code below in it: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Here you extend the core uri class and overwrite 4 of its methods. Because the uri class is automatically loaded, you don't need to load this extended library yourself. How it works? If your URL is like this: my-site.com/news/archive/5 Code: echo $this->uri->segment(2); // echoes 'archive' If your URL is like this: my-site.com/news/this-is-the-first-item/archive/5 Code: echo $this->uri->segment(2); // echoes 'this-is-the-first-item' So you can still use the number based approach, but at the same time you can use the string based approach, wich makes it function like $_GET['archive'] !!! Thanks to xwero again
[eluser]Colin Williams[/eluser]
Quote:Now nobody needs the ?key=value anymore! but everybody can do key/value !!! Well, that of course was always the case. xwero's modification of the segment() method just makes it a bit more elegant.
[eluser]CtheB[/eluser]
[quote author="Colin Williams" date="1242611777"] Quote:Now nobody needs the ?key=value anymore! but everybody can do key/value !!! Well, that of course was always the case. xwero's modification of the segment() method just makes it a bit more elegant.[/quote] Not true. With $_GET you could get the value from the key no mather how many segments: segment1/segment2/segment3?key=value segment1/segment2?key=value with the normal segment() method you couldn't. with the modified you can.
[eluser]Colin Williams[/eluser]
You would typically use the uri_to_assoc() method without xwero's modification. Based on your odd example, I'm not sure you're on the same page with us. $_GET is irrelevant in this discussion.
[eluser]CtheB[/eluser]
uri_to_assoc(4) returns: segment4 => segment5 segment6 => segment7 segment8 => segment9 however, if our key/value changes from segment4/segment5 to segment5/segment6, we can not control this dynamically with uri_to_assoc. With xwero's modification to segment() we can control this, because it doesn't mather if the key is in segment4 or segment5, we still get the correct value. (just like in $_GET)... with xwero's modification, the segment() method acts the same as $_GET, if the parameter is a string. The only difference is now you have key1/value1/key2/value2 instead of ?key1=value1&key2=value2. $this->uri->segment('archive') returns the same result as $_GET['archive'] no mather wich segment the archive is. Example: my-site.com/news/archive/5 my-site.com/news/this-is-the-first-item/archive/5 is the same as: my-site.com/news?archive=5 my-site.com/news/this-is-the-first-item?archive=5 with xwero's modification, we could get the 5 without telling the method at wich segment the key/value pair starts, wich is the same as $_GET acts...
[eluser]Colin Williams[/eluser]
I see where you're headed with that. I don't always buy into the "what happens when the URI gets a mind of its own" scenario, but like I said, xwero's modification is elegant. |
Welcome Guest, Not a member yet? Register Sign In |