Welcome Guest, Not a member yet? Register   Sign In
URI routing question


I'm new to codeigniter but a big fan already. I have a question on how to make something work related to URI routing.

I have a button on my site that redirects a user to an external site with a callback url. I cannot control the use of query strings in the callback url format. The callback url looks something like:

I'm trying to handle this type of query and have it call a "processAuth" function on "mycontroller". So I need to map the above callback to something like:


The above call to processAuth works fine.

I can't figure out how to use URI routing with reqular expressions to solve this problem. I tried adding various routes like:

$route['mycontroller?auth_token=(.+)'] = "mycontroller/processAuth/$1";
$route['mycontroller\?auth_token=(.+)'] = "mycontroller/processAuth/$1";
$route['(mycontroller\?auth_token=)(.+)'] = "mycontroller/processAuth/$2";

Can someone help me out? I'm not even sure I'm on the right track...is there a better/easier way?

Thanks in advance.

Quote:I cannot control the use of query strings in the callback url format. The callback url looks something like:

Do you mean you don't have control at all and must deal with this format, that is an outer space URL to CI?

I guess what you get with
is a 404 because CI then looks for a controller named auth_token (without query strings enabled).
CI unsets the $_GET array if you don't "enable query strings" (see libraries/Input.php) and I don't think you can grab the value, even in the triggered auth_token controller.

If you enable query strings, then refer to the docs at section Enabling Query Strings. I didn't have much good results even with this...

Please give feedback!

EDIT: I didn't see it was your first post. Welcome here! And for these kind of problems, if you have a local install, don't be afraid to hard-debug CI to understand how it works. When investigating for your problem, I just added some
in libraries/Router.php and so on to see how CI behaves.

[eluser]Craig A Rodway[/eluser]
You could probably pass this auth token as a final URI segment with some creative regular expressions in htaccess (using mod_rewrite) instead of in the route.

Yes, I don't have total control over the callback URL:

I only control the callback part of it:

The external site I'm calling adds:

And I can't control how they pass the auth_token back to me, so I can't make the callback look like:

I'll try to figure out how to use htaccess to rewrite the URL. I'm not familiar with htaccess but will try my best.

I'll look into "enabling query strings" if that doesn't work.

Thanks for the responses. I'll let you know how it goes. Please let me know if you have any other suggestions/ideas.

After more investigations: without enabling query string,
triggers the key controller. In its index() function, you can access the value val in $_REQUEST['key'].

[eluser]Craig A Rodway[/eluser]
Ok, I've been playing around with this a bit and after some testing it seems that you can get this to work if you change the URI protocol in config.php to PATH_INFO and use something like this mod_rewrite:

RewriteEngine on
RewriteBase /

RewriteCond $1 !^(index\.php|web)
RewriteCond %{QUERY_STRING} auth_token=(.*)$
RewriteRule ^(.*) index.php/$1/auth/%1 [L]

RewriteCond $1 !^(index\.php|web)
RewriteRule ^(.*)$ index.php/$1


would make:


If you don't change it to PATH_INFO, it looks like the query string upsets the auto-detection. Also, with default routing in place, if you don't specify a controller and a function it won't work either.

I just noticed the last few responses but wanted to tell you what I did in the last 10 minutes.

After looking at the documentation for "enabling query strings", I decided to give that a try.

I do have control over the callback url as I described in my last post, so I enabled query strings and changed the callback to:

I changed my processAuth function in my controller to:
function processAuth () {
  echo "hello"

It worked. The processAuth function gets called on the callback and the new callback url is:

So now I just need to figure out how to extract the auth_token value from the processAuth() function. I'll go try to figure that one out now. Smile

Thanks again for the responses.

I made some changes to solve my problem. Check out:

Thanks for your help. Smile

Theme © iAndrew 2016 - Forum software by © MyBB