Part of the EllisLab Network
   
1 of 2
1
Routing/Function Parameter Bug?
Posted: 16 March 2007 03:54 PM   [ Ignore ]  
Grad Student
Rank
Total Posts:  66
Joined  07-12-2006

i am trying to setup a route and then pass the segment/variable to the function as a parameter

i have this route:

$route[':any'] = "welcome/index/$1";

and this function in my welcome controller:

function index($test)
{
    
echo $test;
}

running this echo’s literally “$1”...not the segment value in the url. am i doing something wrong here or is this a bug? thank you.

Profile
 
 
Posted: 18 March 2007 11:34 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

You’re trying to combine regex-style routing and non-regex routing. Try this:

config/routes.php:

$routes[':any'] = 'welcome';

controllers/welcome.php:

function _remap() {
    $seg
= $this->uri->segment(1);
    
$this->index($seg);
    
// Alternately, instead of calling $this->index(),
    // you could just put the code in index() here.
}

See the User Guide for more information about _remap().

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 19 March 2007 03:02 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  66
Joined  07-12-2006

Thanks shadowhand, this cleared things up. Now that you mention it though, i would actually prefer to use regex for routing.

the following works:

$route['([a-zA-Z0-9-]+)'] = "welcome/index/$1";

however if i try to route to a controller in a subdirectory, this stops working…

$route['([a-zA-Z0-9-]+)'] = "subdir/controller/function/$1";

any ideas? thanks again.

Profile
 
 
Posted: 19 March 2007 07:40 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  572
Joined  09-30-2006

You should take this out of the bug forum and into the codeigniter discussion forum.

When you say this stops working, what error are you getting exactly?

Profile
 
 
Posted: 19 March 2007 07:53 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  66
Joined  07-12-2006

yea i just thought it was a bug. and it still might be with this subdirectory issue.

What i meant when i said that it stops working is that in my first example, the first segment gets passed to my function as a parameter as expected. when i try to do the same route, but to a controller/function in a subdirectory, the segment is not passed to my function and i get the following error:

Missing argument 1 for ControllerName::index()

any ideas i would appreciate it. if this is not a bug, how do a take it out of the bug forum and move it over to the ci discussion forum.

thanks.

Profile
 
 
Posted: 20 March 2007 11:52 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

I think I found your issue, it’s at line ~216 of system/codeigniter/CodeIgniter.php:

Replace:

call_user_func_array(array(&$CI, $method), array_slice($RTR->rsegments, (($RTR->fetch_directory() == '') ? 2 : 3)));

With:

$segments = ($RTR->rsegments == $RTR->segments)
          ?
array_slice ($RTR->rsegments, (($RTR->fetch_directory() == '') ? 2 : 3))
          :
array_slice ($RTR->rsegments, 2);
call_user_func_array(array(&$CI, $method), $segments);

I’m not entirely sure what kind of implications this will have, but I ran a bunch of tests and it behaved as expected.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 22 March 2007 06:16 AM   [ Ignore ]   [ # 6 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6762
Joined  03-23-2006

Sorry for joining this party late.

Could we summarize what the problem is, how I can re-create it, and what the proposed fix is?  What are the benefits of the fix, what are the drawbacks?  If we can get a bunch of us to run it problem free we could then drop it into the CI branch.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 22 March 2007 09:08 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Rank
Total Posts:  66
Joined  07-12-2006

Sure Derek.

The Problem: When using regular expressions for routing, the variable cannot be passed to a function as a parameter when the controller is in a subfolder. For an example see below.

Lets say you have 2 identical Welcome controllers. One at application/controllers/welcome.php and one in application/controllers/account/welcome.php.

Welcome::index()

function index($var = 'error')
{
     
echo $var;
     exit;
}

When I setup the below route for the non-subfolder controller, domain.com/test/works echo’s “works” as expected:

$route['test/([a-z]+)'] = "welcome/index/$1";

When I setup the below route for the subfolder controller, domain.com/test/works echo’s “error”. ie…a “Missing argument 1 for Welcome::index()” PHP error:

$route['test/([a-z]+)'] = "account/welcome/index/$1"

Shadowhand proposed a fix which is just above. He said he tested it some, but is not entirely sure of the implications. I will test it further today, but perhaps you may see possible implications.

Thanks again,
Kevin

Profile
 
 
Posted: 22 March 2007 09:44 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

That’s a pretty good summary. I think it has to do with rsegments vs segments vs subdirs. Not really sure how they all come together at that single point, but the array_slice() was taking off 1 segment too much.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 22 March 2007 10:33 AM   [ Ignore ]   [ # 9 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6762
Joined  03-23-2006

Yeah, this is a tough nut to crack.  Are controllers in sub-directories even officially supported?  I know we all use them, but I’m not sure its something that was planned into the code per se.

In just looking at the patch, I can’t see any issues with it, but I’d like a few more people to run with it before we drop it in.

To the community: If you’re using this patch successfully, even if you aren’t using sub-controllers please post into this thread.  If you’re looking for a chance to beta-test a new feature and help CI, this is it!

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 22 March 2007 10:40 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  66
Joined  07-12-2006

Ok I will continue to test this. I assume that this is officially supported:

user_guide
changelog as of v1.4.0

thanks again

Profile
 
 
Posted: 22 March 2007 11:44 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  4
Joined  03-12-2007
Derek Allard - 22 March 2007 10:33 AM

Yeah, this is a tough nut to crack.

Hi!

I found another problem (which is very similar so I decided not to open another thread) with function parameters and routing.

I am using following example: http://codeigniter.com/wiki/Category:Internationalization::Internationalization_Views_i18n/

(in short, in that example url is changed from /Controller/Action/Argument/ to /Lang/Controller/Action/Argument )

And this is the problem:

For url: http://localhost/en/C/A/arg

‘en’ is successfully found as language
‘C’ is successfully found as controller
‘A’ is successfully found as action

but argument ‘arg’ is not! :(

the method ‘A’ receives its name instead of the argument. So it looks like the problem is indeed in CodeIgniter.php:216 and in hardcoded values 2 or 3.

I believe that segments index for accessing arguments should be calculated according to the real position of the arguments in the segment array and not hardcoded.


For now, of course I can ignore action parameter and just access 4th segment in the segment_array but this is not the real solution. Unfortunately I still don’t have enough knowledge of CI to tackle this problem on my own.

Profile
 
 
Posted: 23 March 2007 12:22 AM   [ Ignore ]   [ # 12 ]  
Grad Student
Rank
Total Posts:  66
Joined  07-12-2006

Ok I have been testing this fix and have found a problem. The fix works great for the route we were having a problem with described above.

This works with the fix in place:

$route['signup/(.+)']= "member/signup/index/$1";

However, when I add another “level” to my route, it again stops working:

$route['confirm/email/(.+)'] = "member/confirm/email/$1";

Does the fix not address this perhaps? Thanks again. Sorry this keeps coming up, but it would be really nice to have this working properly. I will look into the fix and see if I can figure anything out as well.

Profile
 
 
Posted: 23 March 2007 12:38 AM   [ Ignore ]   [ # 13 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1740
Joined  06-23-2006
Derek Allard - 22 March 2007 10:33 AM

Are controllers in sub-directories even officially supported?

To answer this question…

Yes, it was an official addition a few versions ago.

 Signature 

Mac OS X 10.4.10, Apache 1.3.3, PHP 5.2.3, CodeIgniter 1.5.x., baby!

Profile
 
 
Posted: 19 April 2007 12:04 PM   [ Ignore ]   [ # 14 ]  
Grad Student
Rank
Total Posts:  51
Joined  09-12-2006

Hi

Look here and tell me if this solve your problem too??
thanks

Profile
 
 
Posted: 19 April 2007 12:28 PM   [ Ignore ]   [ # 15 ]  
Grad Student
Rank
Total Posts:  66
Joined  07-12-2006

Yes that does solve my problem. Just to clarify, the fix is on the following page:

http://codeigniter.com/forums/viewthread/50795/

Perhaps repost it in here. If this is OK’d by the CI team, please include this in the changelog when added.

Thanks.

Profile
 
 
   
1 of 2
1
 
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 719, on June 06, 2008 10:16 AM
Total Registered Members: 66425 Total Logged-in Users: 33
Total Topics: 84784 Total Anonymous Users: 2
Total Replies: 454964 Total Guests: 229
Total Posts: 539748    
Members ( View Memberlist )
Newest Members:  csaturkeytherendStudioGeorgiaJZeerfedegheEdgedcenticeRoger_Mxcamilojoostvaningen