Part of the EllisLab Network
   
 
URI routing question
Posted: 05 January 2008 02:42 PM   [ Ignore ]  
Summer Student
Total Posts:  7
Joined  01-05-2008

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/mycontroller?auth_token=88797abc89798dd444334

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/mycontroller/processAuth/88797abc89798dd444334

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.

Profile
 
 
Posted: 06 January 2008 02:34 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  493
Joined  02-21-2007

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/mycontroller?auth_token=88797abc89798dd444334

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/mycontroller?auth_token=88797abc89798dd444334
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

die(APPPATH.'controllers/'.$segments[0]);

in libraries/Router.php and so on to see how CI behaves.

Profile
 
 
Posted: 06 January 2008 06:12 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  545
Joined  03-08-2006

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.

 Signature 

Twitter | Flickr | Last.fm | Del.icio.us

Profile
 
 
Posted: 06 January 2008 08:04 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  7
Joined  01-05-2008

Yes, I don’t have total control over the callback URL:
http://www.mysite.com/index.php/mycontroller?auth_token=88797abc89798dd444334

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/mycontroller/88797abc89798dd444334


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.

Profile
 
 
Posted: 06 January 2008 08:18 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  493
Joined  02-21-2007

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’].

Profile
 
 
Posted: 06 January 2008 08:18 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  545
Joined  03-08-2006

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

http://www.example.com/welcome/welcome/?auth_token=abcdef123456

would make:

http://www.example.com/welcome/welcome/auth/abcdef123456

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.

 Signature 

Twitter | Flickr | Last.fm | Del.icio.us

Profile
 
 
Posted: 06 January 2008 08:34 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  7
Joined  01-05-2008

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=mycontroller&m=processAuth

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:
http://www.mysite.com/index.php?c=mycontroller&m=processAuth&auth_token=3c562b119bd387ec194ade6bab9832c4

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.

Profile
 
 
Posted: 11 January 2008 02:18 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  7
Joined  01-05-2008

I made some changes to solve my problem.  Check out:
http://codeigniter.com/forums/viewthread/68698/

Thanks for your help. smile

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 115012 Total Logged-in Users: 69
Total Topics: 122446 Total Anonymous Users: 5
Total Replies: 647326 Total Guests: 550
Total Posts: 769772    
Members ( View Memberlist )