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. }
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.
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.
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:
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.
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.
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!
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.
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.