模块化Code​​igniter语言路由

时间:2013-09-07 11:28:50

标签: codeigniter module routes

我正在尝试使用具有模块扩展名的codeigniter进行路由,问题是我想使用这样的seo URL:

/en/controller/action - controller is that in application/controllers
/en/module/controller/action - controller is that in application/modules/module/controllers/

$route['^(en|bg)/(.+)$'] = "$2";
$route['^(en|bg)$'] = $route['default_controller'];

现在正在运行,但模块应该在language / en / module之后有办法改变它吗?

1 个答案:

答案 0 :(得分:2)

您好我在我的问题上找到了解决方案,我将分享它。我将整个国际化进程。

首先我在config.php中添加了

$config['seo_language'] = TRUE; //This will put language in the url
$config['languages'] = array('bg'=>'bulgarian','en'=>'english');

其次我做了钩子,所以我可以为每个控制器自动加载语言 在config.php中

$config['enable_hooks'] = TRUE;

并在config / hooks.php中注册我们的钩子

$hook['post_controller_constructor'] = array(
    'class' => 'LanguageLoader',
    'function' => 'initialize',
    'filename' => 'LanguageLoader.php',
    'filepath' => 'hooks'
);

所以现在钩子/文件夹中的类LanguageLoader.php

class LanguageLoader
{
    function initialize() {

        $ci =& get_instance();
        $ci->load->helper('language');

        $class = $ci->router->class;

        if($class!='language'){

            //Get all available languages which are set in our config file.
            $languages = config_item('languages'); 

            /* Try all */
            //Try url
            $module = $ci->router->fetch_module();
            $position = !empty($module) ? 2 : 1;
            $url_lang = trim(strtolower($ci->uri->segment($position))); 
            //Try session
            $session_lang = $ci->session->userdata('site_lang');
            //Try post
            $post_lang = @$_POST['site_lang'];
            //Try cookie
            $cookie_lang = $ci->input->cookie('site_lang');

            //The lang with highest priority is that one which is coming from url
            $found = false;
            if(!empty($url_lang)){ 
                if(array_key_exists($url_lang, $languages)){ 
                    $lang = $url_lang;
                    $found = true; 
                    if($cookie_lang!=$url_lang){
                        //Set it in cookie and session, so we can used later if is not in the url
                        $cookie = array(
                            'name'   => 'site_lang',
                            'value'  => $url_lang,
                            'expire' =>  7*24*86500,
                            'secure' => false
                        );
                        $ci->input->set_cookie($cookie);
                        //add lang in session as well
                        $ci->session->set_userdata('site_lang', $url_lang);
                    }
                }               
            }
            //Second is post cookie
            if(!$found and !empty($cookie_lang)){ 
                if(array_key_exists($cookie_lang, $languages)){ 
                    $lang = $cookie_lang;
                    $found = true;
                }               
            }           
            //Third one is post
            if(!$found and !empty($post_lang)){ 
                if(array_key_exists($post_lang, $languages)){ 
                    $lang = $post_lang;
                    $found = true;
                }               
            }
            //Last check in the session 
            if(!$found and !empty($session_lang)){ 
                if(array_key_exists($session_lang, $languages)){ 
                    $lang = $session_lang;
                    $found = true;
                }               
            }           
            //If still we dont have language, load default one
            if(!$found){

                $default_language = config_item('language');
                $lang = array_search($default_language, $languages);

                 $ci->load->language($class, $default_language);

            }else{

                 $ci->load->language($class, $languages[$lang]);

            }

            //Define global variable we need this for our view helper to build our seo links
            define('SITE_LANG', $lang);

        }
    }

}

接下来是替换site_url函数。在应用程序/核心/ 如果您不使用模块化扩展,请删除前2行并取消注释第3行

require APPPATH."third_party/MX/Config.php";

class MY_Config extends MX_Config {

//class MY_Config extends CI_Config {

    function site_url($uri = '')
    {    
        if (is_array($uri))
        {
            $uri = implode('/', $uri);
        }

        if (function_exists('get_instance'))        
        {
            $CI =& get_instance();
            $uri = $CI->lang->localized($uri);            
        }

        return parent::site_url($uri);
    }

}

在application / helpers中替换lang helper my_language_helper.php

 function lang($line, $id = '')
 {
  $CI =& get_instance();
  $line = $CI->lang->line($line);

  $args = func_get_args();

  if(is_array($args)) array_shift($args);

  if(is_array($args) && count($args))
  {
      foreach($args as $arg)
      {
          $line = str_replace_first('%s', $arg, $line);
      }
  }

  if ($id != '')
  {
   $line = '<label for="'.$id.'">'.$line."</label>";
  }

  return $line;
 }

 function str_replace_first($search_for, $replace_with, $in)
 {
     $pos = strpos($in, $search_for);
     if($pos === false)
     {
         return $in;
     }
     else
     {
         return substr($in, 0, $pos) . $replace_with . substr($in, $pos + strlen($search_for), strlen($in));
     }
 }

在routers.php中的最后一个

$route['^(en|bg)/(.+)$'] = "$2";
$route['^(en|bg)$'] = $route['default_controller'];

//MODULE ADMIN - It Has to be added for each module
$route['admin/(en|bg)'] = 'admin';
$route['admin/(en|bg)/(:any)'] = 'admin/$2';
$route['admin/(:any)'] = 'admin/$1';

我希望如此,这将有助于任何想要在不使用i18n的情况下将其网站国际化的人