php codeigniter3基于多语言应用的url缩写与路由别名冲突

mbzjlibv  于 2023-03-21  发布在  PHP
关注(0)|答案(1)|浏览(124)

我有一个基于codeigniter v3的应用程序,有5种语言(ar,en,ru,es,fr),阿拉伯语是默认的,这是(添加资金)页面链接:

app.com/add_funds  
app.com/en/add_funds
app.com/ru/add_funds
app.com/es/add_funds
app.com/fr/add_funds

一切工作如预期,但当我设置了一些路由404的别名!
$route['deposit'] = 'add_funds/index';

app.com/add_funds          // ok
app.com/en/add_funds       // ok
app.com/ru/add_funds       // ok
app.com/es/add_funds       // ok
app.com/fr/add_funds       // ok

app.com/deposit            // ok
app.com/en/deposit         // 404
app.com/ru/deposit         // 404
app.com/es/deposit         // 404
app.com/fr/deposit         // 404

我的代码:
routes.php:

$route['default_controller']       = 'home';
$route['(\w{2})/(.*)']             = '$2';
$route['(\w{2})']                  = $route['default_controller'];
$route['404_override']             = 'custom_page/page_404';
$route['translate_uri_dashes']     = false;
$route['deposit']                  = 'add_funds/index';

.htaccess权限:

RewriteEngine On
Options +FollowSymLinks
Options -Indexes
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . index.php [L,QSA] 

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

基本控制器功能:

function init_app_lang(){
        
    global $URI;
    $CI = &get_instance();
    $supported_langs = ['en'=>'english','ru'=>'russian','es'=>'spanish','fr'=>'french'];
        
    $uri_abbr = $URI->segment(1);
    if ( isset($supported_langs[$uri_abbr]) && strlen($uri_abbr) == 2 ) {

        $langDefault = $supported_langs[$uri_abbr];
        set_session('langCurrent', $langDefault);

    }else{

        $langDefault = "arabic";
        set_session('langCurrent', $langDefault);

    }

}

谢谢&对不起我的英语

0qx6xfy6

0qx6xfy61#

通过从路由数组中通过$值替换$瓦尔($alias key)变量,修复了此问题

if( isset( $this->routes[$val]) ){
    $this->_set_request(explode('/', $this->routes[$val]));
}else{
    $this->_set_request(explode('/', $val));
}

Router.php

/**
     * Parse Routes
     *
     * Matches any routes that may exist in the config/routes.php file
     * against the URI to determine if the class/method need to be remapped.
     *
     * @return  void
     */
    protected function _parse_routes()
    {
        // Turn the segment array into a URI string
        $uri = implode('/', $this->uri->segments);

        // Get HTTP verb
        $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';

        // Loop through the route array looking for wildcards
        foreach ($this->routes as $key => $val)
        {
            // Check if route format is using HTTP verbs
            if (is_array($val))
            {
                $val = array_change_key_case($val, CASE_LOWER);
                if (isset($val[$http_verb]))
                {
                    $val = $val[$http_verb];
                }
                else
                {
                    continue;
                }
            }

            // Convert wildcards to RegEx
            $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);

            // Does the RegEx match?
            if (preg_match('#^'.$key.'$#', $uri, $matches))
            {
                
                
            
                
                
                // Are we using callbacks to process back-references?
                if ( ! is_string($val) && is_callable($val))
                {
                    // Remove the original string from the matches array.
                    array_shift($matches);

                    // Execute the callback using the values in matches as its parameters.
                    $val = call_user_func_array($val, $matches);
                }
                // Are we using the default routing method for back-references?
                elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
                {
                    
                    $val = preg_replace('#^'.$key.'$#', $val, $uri);
                    
                    if( isset( $this->routes[$val]) ){
                        $this->_set_request(explode('/', $this->routes[$val]));
                    }else{
                        $this->_set_request(explode('/', $val));
                    }
                    
                return;
            }
        }

        // If we got this far it means we didn't encounter a
        // matching route so we'll set the site default route
        $this->_set_request(array_values($this->uri->segments));
}

相关问题