• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alternate URI syntax

I'm using this function:


It looks like the same thing, just a lot less code ;-)

[eluser]Colin Williams[/eluser]
I'm just not sure where uri_to_assoc fails? Looks like you just replaced / with :

The purpose of the library is so that you can pass parameters through the URI, without having to put them in a certain order. For example, take a search function. There could easily be at least 5 different parameters, which may be optional. Using uri_to_assoc just seems too hit-and-miss to me. If the user removes a single segment, the entire associative array will be messed up.

You're right. I have essentially replaced '/' with ':'. In fact, I actually use a ':' instead of a '=', like you see in query strings. By replacing the slash with a colon, it makes it easier to treat the parameters as segments, and identify a key/value pair.

[quote author="codex" date="1237610559"]I'm using this function:


It looks like the same thing, just a lot less code ;-)[/quote]

Hi. I've never seen this helper before. Although the fact that it works in a similar fashion to mine makes me happy that I was on the right track.

You're right, though. It's quite similar to what I started with, I just felt that I should be able to work with the uri object, just like we can in CodeIgniter.

Here's where my library differs:

My library generates the associative array once only, and it does it automatically.

My library allows you to specify a default value if the segment you're after does not exist.

You don't have to work with an array, so no messing with "isset()", the library returns FALSE if you're calling upon a segment that doesn't exist.

My library also allows you to create a custom URL easily from an associative array. Granted, you can probably do this manually just as easily, but it just made sense to me to have a method that created URLs, as well as one that dis-assembled them.

My library automatically urlencode()s strings that are to be send via get. For those who have problems with the server decoding forward slashes automatically, you can use my [url="http://ellislab.com/forums/viewthread/107741/"]URI-safe base64 encode/decode helpers[/url].

Thanks for your comments.

When i discovered the $_SERVER['QUERY_STRING'] isn't touched i need solutions like this even less than i did before. These are all ways to patch the missing GET functionality. Clean urls are nice but sometimes there are more an obstruction than a blessing.

Using GET you don't need to use base64 encode/decode, you don't need to separate the keys from the values, the creation of the url is done by sending form.

If people use this solution i'm not going to stop them, they will have their reasons. I just want to make them aware of the less resource needing alternative

OK, I should have pointed out earlier that this library is not intended to be a replacement for GET. I designed this because a single method of my application (a search function) accepts multiple optional parameters that could be in any given order. I wanted to make searches bookmarkable, and I didn't want to have to make the Web site use query strings on account of a single function call. My library helps with these situations.

What I'm saying is that, sure, whilst query strings are plain f'ugly, they do give you undeniable flexibility with regards to the order of the parameters you supply. My library is a bridge between both query strings, and segmented URIs. It allows you to utilise one of the best features of query strings, and still allows you to keep your URLs looking clean. It's not a replacement for query strings, but rather a supplement for segmented URIs. It's not necessarily meant to be used in every URL, but those URLs where you need more flexibility.

Thanks for your comments.

How would my controller look like to fetch these params?

You'd use $this->uri->asegment('key_name');

So if you're URI looked something like this:


$username = $this->uri->asegment('username');
$sex = $this->uri->asegment('sex');

should do the trick.

Does that help?

This helps for sure.
But it doesn't echo my value:


class Welcome extends Controller {

    function __construct()
    function testing()
        //echo 'remap';
        echo $this->uri->asegment('keyword');

My url: http://localhost:8888/orange/backend/index.php/welcome/testing/keyword:test

I dont get any errors.

Hmm, you may have discovered a bug. What happens when you use this URL:


using the same code?

It might also be worth doing a var_dump() on the return value.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.