EllisLab text mark
Advanced Search
     
Bug for routing sub-foldered controller [includes patch]
Posted: 25 October 2007 12:55 PM   [ Ignore ]
Joined: 2007-10-25
1 posts

Hi everybody,

I came upon this bug while developing a branch of my application for facebook and needed to put my facebook controllers in a subfolder.

The problem is with Router segments array, in my case it looks like this:

1)“raw” segments (before parsing routes), “facebook” is my sub-folder

array(4{ [0]=> string(8"facebook" [1]=> string(11"<CLASS>" [2]=> string(5"<METHOD>" [3]=> string(7"<PARAM>" 

2) routed segments ($Router->rsegments)

array(3{ [0]=>  string(11"<CLASS>" [1]=>  string(5"<METHOD>" [2]=>  string(7"<PARAM>" 

and surprisingly the first one is served in the end, so it looks like routing didn’t happen..


In Router.php there’s a method _reindex_segments() (line 252) where you check :

array_diff($this->rsegments$this->segments

And here’s the bug.. even though on php.net (http://pl.php.net/manual/en/function.array-diff.php) they say that array_diff “Compares array1 against array2 and returns the difference.” it doesn’t work like this.. instead it returns an array containing all the values of array1 that are not present in any of the other arguments.


So in my case your check returns $diff = false even if arrays are different.


My patch looks like this :

$diff = (count(array_diff($this->rsegments$this->segments)) == && count(array_diff($this->segments$this->rsegments)) == 0) ? FALSE TRUE


And it works perfectly like this smile

Best,
Andrzej

 
Posted: 18 November 2007 05:53 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-10-16
3 posts

This fixed a problem I was having as well, thanks! When rerouting, my actions were getting passed the wrong segment.

Edit: After some more digging, this is a pretty well known bug (that is still unfixed).
See threads:
http://ellislab.com/forums/viewthread/50795/
http://ellislab.com/forums/viewthread/48779/

And the bug report:
http://codeigniter.com/bug_tracker/bug/2849/

The bug report is more about the current SVN version than the release version.

To fix this, I’ve replaced

$diff = (count(array_diff($this->rsegments$this->segments)) == 0) ? FALSE TRUE

with

$diff = (count(array_diff($this->segments$this->rsegments)) == 0) ? FALSE TRUE

And added this:

if($this->fetch_directory() != ''{
    array_unshift
($segments,str_replace("/","",$this->fetch_directory()) );

To line 181 in Router.php