Welcome Guest, Not a member yet? Register   Sign In
Bug for routing sub-foldered controller [includes patch]
#1

[eluser]Unknown[/eluser]
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
Code:
array(4) { [0]=> string(8) "facebook" [1]=> string(11) "<CLASS>" [2]=> string(5) "<METHOD>" [3]=> string(7) "<PARAM>" }

2) routed segments ($Router->rsegments)
Code:
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 :
Code:
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 :
Code:
$diff = (count(array_diff($this->rsegments, $this->segments)) == 0 && count(array_diff($this->segments, $this->rsegments)) == 0) ? FALSE : TRUE;


And it works perfectly like this Smile

Best,
Andrzej
#2

[eluser]Unknown[/eluser]
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
Code:
$diff = (count(array_diff($this->rsegments, $this->segments)) == 0) ? FALSE : TRUE;
with
Code:
$diff = (count(array_diff($this->segments, $this->rsegments)) == 0) ? FALSE : TRUE;

And added this:
Code:
if($this->fetch_directory() != '') {
    array_unshift($segments,str_replace("/","",$this->fetch_directory()) );
}
To line 181 in Router.php




Theme © iAndrew 2016 - Forum software by © MyBB