views:

526

answers:

7

Hey guys, 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.

A: 

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";
watain
+1  A: 

Any Routes using RegEx must be placed after the reserved routes of scaffolding_trigger and default_controller this is most likely your problem.

Zack
Sadly I did this.
Lukas Oppermann
+1  A: 

or try $route[':any/login'] = "/user/index";

P.M
This seems to work now. I do not know why, but regular expressions do not work at all. Thanks anyway.
Lukas Oppermann
A: 

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.

Lukas Oppermann
A: 

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";
Xeoncross
It seems to not work as well. Would you need me to provide any more information? Please let me know how I could make it easier to help me, if there is any help.Thanks
Lukas Oppermann
I think the problem is, that CI does not like me using regex and a normal name. So I would need to tell "login" in regex as well. Sadly I am totally stupid when it comes to regex, so how would I do this?
Lukas Oppermann
A: 

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';
Maurice Kherlakian
+1  A: 

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.

Lukas Oppermann