Welcome Guest, Not a member yet? Register   Sign In
Removing section from URI then continuing to controller/method
#1

[eluser]MikePearce[/eluser]
Hi all!

I need to append a key:value pair to my URI:

Code:
http://site.com/somecontroller/somemethod/id:1234

id:1234 could be on the end of any URI on the site (it's a replacement for the querystring). It's always going to be on the end, but I'll not know WHERE it is in the URI, so it might be
Code:
http://site.com/somecontroller/id:1234
or
http://site.com/id:1234

So, my question is, how do I remove it from the URI, process it and continue on my way?

I thought about using a pre controller hook, strip it from the URI, process it (I need to set the ID in the cookie and do some other tracking) and then set whatever variable CI needs to know which controller/method to run. When I use get_instance() in a pre-controller hook, I get nothing! Even if I did get something, I don't know what the vars are that I need.

Anyone got any ideas?

Thanks,

Mike
#2

[eluser]Michael Wales[/eluser]
Why are you wanting to remove it from the URI? Seems like a bit of obscurity to me and we all know obscurity != security.

I would simply count the number of segments on my URI, retrieve the value of the last segment, and call it good - no reason to hide it (your users are going to see it anyways).
#3

[eluser]MikePearce[/eluser]
Hi Walesmd.

Thanks for the reply. I did think about doing that, but I'll never know whats before the key:value pair. There might be a controller, method and params, or not or a combination. So CI won't know what to do.

I'm not fussed about whether the pair appears in the URL or not, I just need to be able to remove them from the URL so that CI knows where to route, as with out the pair, the URI would be well formed and CI would know what to do.

Ex:
http://site.com/acontroller/amethod/param1/key:value
http://site.com/acontroller/amethod/key:value

With the second URI, CI thinks the key:value pair is param1. I don't want to have to write something into every method to deal with this, so I thought I would work it out first, process the pair and then strip from the URL so CI knows what to do.

Cheers,

Mike
#4

[eluser]gunter[/eluser]
Code:
$segs = $this->uri->segment_array();
$your_last_segment = array_pop($segs);

this should give you the last segment
#5

[eluser]Michael Wales[/eluser]
What does this id:1234 represent, maybe we can suggest a better way to go about this?
#6

[eluser]MikePearce[/eluser]
Gunter:

Thanks, but what happens to CI routing in this example?
http://site.com/acontroller/amethod/param1/key:value
http://site.com/acontroller/amethod/key:value

walesmd:

Basically, there is a legacy system that directs traffic to any page on the site, and it will contain ?id=1234 as a query string, I need to be able to deal with that no matter where it appears. It may also contain other key/value pairs in the QS, but I'll deal with that later.
#7

[eluser]gunter[/eluser]
the first line puts the segments into an array
first example (pseudo code)
$segs[0]="acontroller"
$segs[1]="amethod"
$segs[2]="key:value"

second example:
$segs[0]="acontroller"
$segs[1]="amethod"
$segs[2]="param1"
$segs[3]="key:value"

then array_pop($segs) returns the last element of $segs...
read in the php manual about array_pop
so if your segment is always the last one then array_pop would be my choice
#8

[eluser]Neovive[/eluser]
Perhaps you could setup some mod_rewrite rules in Apache to rewrite the legacy id to your new CI structure.
#9

[eluser]MikePearce[/eluser]
Gunter:

I get it now - sorry, was being dumb. How do I then put those segments, minus the one I've popped off, back into the CI routing structure, so that key:value doesn't futz with the routing, BEFORE CI starts to route? That, I think, is the hard part!
#10

[eluser]gunter[/eluser]
:ahhh: donĀ“t panic, Mike!

there are two almost similar functions:
$this->uri->uri_string()
Returns a string with the complete URI. For example, if this is your full URL:

$this->uri->ruri_string(n)
This function is identical to the previous one, except that it returns the re-routed URI in the event you are using CodeIgniter's URI Routing feature.

I think $this->uri->uri_string() will bring you the original uri string.




Theme © iAndrew 2016 - Forum software by © MyBB