CodeIgniter Forums

Full Version: Route URL with GET parameters to cleaner URL
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]vendiddy[/eluser]
Hi, I am trying to figure out how to reroute a url like application/test?token=123 to a url like application/test/123. This is because the Zend_Gdata library needs to send an authentication token back to a method of my controller in the form of a GET parameter.

Code:
function test_gcal_auth()
{
    $this->load->library('zend');
    $this->zend->load('Zend/Gdata/Calendar');
    $this->zend->load('Zend/Gdata/AuthSub');        
    $scope = 'http://www.google.com/calendar/feeds/';
    $secure = true;
    $session = true;
    $next = $this->config->item('base_url') . 'system/umdcal/test.php';
    $url = Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, $session);
    echo sprintf('<a href="%s">Login</a>', $url);
}

I currently use a hack where I set the next url to be test.php. In test.php, I extract the GET parameter and redirect to the correct method in my application controller. (As seen below.)

Code:
$token = $_GET['token'];
header("Location: http://localhost/umdcal.php/application/test/$token");

Does anyone know of a cleaner solution? I tried adding a regex pattern in routes.php but wasn't able to get it working.

Thanks!

El Forum

[eluser]Phil Sturgeon[/eluser]
Im confused about what your trying to achieve and where you are trying to achieve it.

You seem to have /application/ and /system/ in your URL's. This is not normal CI behaviour.

Anyway, the following code would work:

Code:
&lt;?php
class Whatever extends Controller() {

    function method($token = '') {
        $this->load->helper('url');
        redirect('application/test/'.$token);
    }

}
?&gt;

If you could explain your code more I could help you further Smile

El Forum

[eluser]vendiddy[/eluser]
Sorry for the confusion, but my controller is named application. (Is there a standard for naming the default controller?)

Basically, if I paste this URL into my browser:
http://localhost/umdcal.php/application/test?token=CPPu5rz7ABCCf-Tq_v____8B
I get the 404 page even though I have a function called test in my application controller.

Instead of getting this 404 page, I would like for the above URL to automatically redirect to
http://localhost/umdcal.php/application/test/CPPu5rz7ABCCf-Tq_v____8B

I don't think I can use an alternative to the GET parameter. This is because Google's authentication works by redirecting to a page of my choice and attaching the token as a GET parameter.

Thanks again!

El Forum

[eluser]drewbee[/eluser]
Hi Vendiddy,

I set up a referral system on one of my CI sites, which allowed the user to take any page / controller / method etc and attach a ?ref=userid to it.

What I did assumes that query_string is still set to OFF for CI.

in the pre_controller hook, you can still process $_GET as CI hasn't gotten ahold of it yet.

Here is what I would do: at this point in time there are only two classes loaded for CI, so you have to use native PHP code for everything
I would actually recomend only having this run on the page that can accept this as well.
pre_controller hook:
Code:
if (isset($_GET['token']))
{
    header("Location: /umdcal.php/application/test/" . urlencode($_GET['token']));
    unset($_GET['token']);
}

... or if you think of something else besides a header redirect. Make sure you do the unset() though, that way CI doesn't see it, as it would throw a 404 as you are currently experiencing.

El Forum

[eluser]drewbee[/eluser]
One other thing to try.

What happens if you feed google an end slash (after test)

IE
http://localhost/umdcal.php/application/test/

Does CI then pass:
?token=CPPu5rz7ABCCf-Tq_v____8B

to the controller? Or does it try and make a url out of it?

IF it does pass that, you could simply strip the ?token= off of it and you have your token string.


This would, of course, require you to add additional 'allowed uri characters'

El Forum

[eluser]Unknown[/eluser]
Hi,

I'm having the same problem. Is there another workaround that doesn't have to use any hook ? I'm afraid it will have performance problem.

Thanks,

JarBis