CodeIgniter Route Problem with Regex
I have got a little problem using the CodeIgniter route function.
I use the URI_Language_Identifier extension and I want to reroute all the requests for "lang/login" (e.g. en/login or de/login) to user/login I tried to use the routes function as follows, but it does not work:
$route['(\w{2})/login'] = "/user/index";
this however does work:
$route['en/login'] = "/user/index";
$route['de/login'] = "/user/index";
but the working version is pretty bad, it will produce redundant code and you have to change to much if you want to add开发者_开发技巧 a new language.
I hope someone has an intelligent answer, as usual. Thanks in advance.
or try $route[':any/login'] = "/user/index";
The real error & solution
Hey, just if someone has the same problem, I found the real error.
The is a line in the route.php
//route example: http://domain.tld/en/controller => http://domain.tld/controller
$route['(\w{2})/(.*)'] = '$2';
$route['(\w{2})'] = $route['default_controller'];
This is from the extension.
You need to put all your routes before this, like in the following:
$route['(\w{2})/signup'] = "user/signup";
//route example: http://domain.tld/en/controller => http://domain.tld/controller
$route['(\w{2})/(.*)'] = '$2';
$route['(\w{2})'] = $route['default_controller'];
Thanks for all the help in this post anyway. You are great.
Any Routes using RegEx must be placed after the reserved routes of scaffolding_trigger
and default_controller
this is most likely your problem.
Try something like
$route['.+/login'] = "/user/index";
since you don't need the matched language code anyway.
I guess something's wrong with the interpretation of your (\w{2})
expression, maybe you'd also have to try something like:
$route['[a-z]+/login'] = "/user/index";
All the 3 do not work for me.
Just providing some more information: I am running it on a local server.
Also I have an .htaccess file with the following code:
AddCharset utf-8 .css .html .xhtml
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ /www/veare/index.php/$1 [L]
I get a 404 error.
Hope you have some more ideas. Thanks.
Here is the code that handles the REGEX part of the routing:
// Loop through the route array looking for wild-cards
foreach ($this->routes as $key => $val)
{
// Convert wild-cards to RegEx
$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
// Does the RegEx match?
if (preg_match('#^'.$key.'$#', $uri))
{
// Do we have a back-reference?
if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
{
$val = preg_replace('#^'.$key.'$#', $val, $uri);
}
$this->_set_request(explode('/', $val));
return;
}
}
I would try
$route['\w\w/login'] = "/user/index";
The following works for me (Provided the language is always lowercase):
$route['([a-z]{2})/login'] = '/user/index';
As Watain mentioned, since you don't need the matched lang. code, you can drop the '(' and ')'
$route['[a-z]{2}/login'] = '/user/index';
精彩评论