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

[eluser]freshmug[/eluser]
Hi,

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:
http://www.mysite.com/index.php/mycontro...98dd444334

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:

http://www.mysite.com/index.php/mycontro...98dd444334

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

[eluser]Grahack[/eluser]
Quote:I cannot control the use of query strings in the callback url format. The callback url looks something like:

http://www.mysite.com/index.php/mycontro...98dd444334
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
http://www.mysite.com/index.php/mycontro...98dd444334
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
Code:
die(APPPATH.'controllers/'.$segments[0]);
in libraries/Router.php and so on to see how CI behaves.
#3

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

[eluser]freshmug[/eluser]
Yes, I don't have total control over the callback URL:
http://www.mysite.com/index.php/mycontro...98dd444334

I only control the callback part of it:
http://www.mysite.com/index.php/mycontroller

The external site I'm calling adds:
?auth_token=88797abc89798dd444334

And I can't control how they pass the auth_token back to me, so I can't make the callback look like:
http://www.mysite.com/index.php/mycontro...98dd444334


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

[eluser]Grahack[/eluser]
After more investigations: without enabling query string,
http://test.local/ci/index.php/test?key=val
triggers the key controller. In its index() function, you can access the value val in $_REQUEST['key'].
#6

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

Code:
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

http://www.example.com/welcome/welcome/?...cdef123456

would make:

http://www.example.com/welcome/welcome/a...cdef123456

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

[eluser]freshmug[/eluser]
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:
http://www.mysite.com/index.php?c=mycont...rocessAuth

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

It worked. The processAuth function gets called on the callback and the new callback url is:
http://www.mysite.com/index.php?c=mycont...6bab9832c4

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

[eluser]freshmug[/eluser]
I made some changes to solve my problem. Check out:
http://ellislab.com/forums/viewthread/68698/

Thanks for your help. Smile




Theme © iAndrew 2016 - Forum software by © MyBB