Welcome Guest, Not a member yet? Register   Sign In
A more-useful uri_to_assoc
#1

[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
// blog/search/category/music/tag/rap
// The controller is blog
// the method used is search

// returns an associative array of all segments:
$this->uri->get()

(array) [category] => music, [tag] => rap

// returns a single value, NULL if segment doesn't exist
$this->uri->get('category');

(string) music

Because URI is always loaded, you can do the following, too, inside your libraries without including the CI instance:

Code:
$vars = MY_URI::get();
$category = MY_URI::get('category');

Personally, I think this functionality ought to be added to the Core.
#2

[eluser]Colin Williams[/eluser]
I dig. Maybe not get() though. Mixing metaphors, so to speak. Maybe param()?
#3

[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)
{
    if(is_string($n) && ($key = array_search($n,$this->segments)) !== false)
    {
       $n = $key+1;
    }
    
    return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
}
The _get_vars method of the extended class does some strange things to end up with the same value as the default value of the ®uri_to_assoc method? Maybe i'm not awake enough to understand it.
#4

[eluser]Colin Williams[/eluser]
That's even better, xwero. Good call.
#5

[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');

class MY_URI extends CI_URI {
    
    public function __construct()
    {
        parent::CI_URI();
    }
    
    public function segment($n, $no_result = FALSE)
    {
        if(is_string($n) && ($key = array_search($n,$this->segments)) !== false)
        {
            $n = $key+1;
        }
        return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
    }

    public function rsegment($n, $no_result = FALSE)
    {
        if(is_string($n) && ($key = array_search($n,$this->rsegments)) !== false)
        {
            $n = $key+1;
        }
        return ( ! isset($this->rsegments[$n])) ? $no_result : $this->rsegments[$n];
    }

    public function slash_segment($n, $where = 'trailing')
    {
        if(is_string($n) && ($key = array_search($n,$this->segments)) !== false)
        {
            $n = $key+1;
        }
        return $this->_slash_segment($n, $where, 'segment');
    }
    
    public function slash_rsegment($n, $where = 'trailing')
    {
        if(is_string($n) && ($key = array_search($n,$this->rsegments)) !== false)
        {
            $n = $key+1;
        }
        return $this->_slash_segment($n, $where, 'rsegment');
    }
}

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'

echo $this->uri->segment('archive');  // echoes '5'

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'

echo $this->uri->segment('archive');  // echoes '5'

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 againSmile
#6

[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.
#7

[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.
#8

[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.
#9

[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...
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB