简体   繁体   English

正则表达式的CodeIgniter路由问题

[英]CodeIgniter Route Problem with Regex

I have got a little problem using the CodeIgniter route function. 我在使用CodeIgniter路由功能时遇到了一些问题。

I use the URI_Language_Identifier extension and I want to reroute all the requests for "lang/login" (eg en/login or de/login) to user/login I tried to use the routes function as follows, but it does not work: 我使用URI_Language_Identifier扩展名,并且我想将对“ lang / login”的所有请求(例如,en / login或de / login)重新路由到用户/登录名,我试图按以下方式使用routes函数,但它不起作用:

$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. 提前致谢。

或尝试$ 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.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. 你很棒。

任何使用RegEx的路由都必须放在scaffolding_triggerdefault_controller的保留路由之后,这很可能是您的问题。

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: 我想您的(\\w{2})表达式的解释有问题,也许您还必须尝试以下方法:

$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: 我也有一个带以下代码的.htaccess文件:

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. 我收到404错误。

Hope you have some more ideas. 希望你有更多的想法。 Thanks. 谢谢。

Here is the code that handles the REGEX part of the routing: 这是处理路由的REGEX部分的代码:

// 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. 正如Watain所述,因为您不需要匹配的lang。 code, you can drop the '(' and ')' 代码,您可以删除'('和')'

$route['[a-z]{2}/login'] = '/user/index';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM