2013-04-01 66 views
0

即时通讯有一个使用codeigniter开发的网站。我的URL strcuture有大约4段和超过500页。目前我的网址有下划线(_),但我需要更换其中破折号( - )用codeigniter URL结构中的破折号替换下划线

当前结构

mywebsite.com/app/standalone_apps/high_runner_beta/download 

所需的URL结构

mywebsite.com/app/standalone-apps/high-runner-beta/download 

我不能用户在CI的路由功能因为有太多的链接需要重定向。

我确实试过 Codeigniter Routes regex - using dashes in controller/method names 的解决方案,但它没有奏效。

有人可以提出一种方法,我可以用我的网址上的破折号替换下划线。

+1

你能够使用的.htaccess这个?如果是这样,请看[这里](http://stackoverflow.com/a/2637044/844726)。 – swatkins

+0

as @swatkins说,.htaccess是你需要看看的地方 –

+0

@swatkins会试试这个解决方案..只是为了知道,$ 1- $ 2是什么意思? – LiveEn

回答

2

这项工作对我来说 - >创建一个PHP文件:MY_Route.php 与此代码,并上传到“应用程序/核心”目录:

<?php class MY_Router extends CI_Router { 
function set_class($class) 
{ 
    $this->class = str_replace('-', '_', $class); 
} 

function set_method($method) 
{ 
    $this->method = str_replace('-', '_', $method); 
} 

function set_directory($dir) { 
    $this->directory = $dir.'/'; 
} 

function _validate_request($segments) 
{ 
    if (count($segments) == 0) 
    { 
     return $segments; 
    } 

    // Does the requested controller exist in the root folder? 
    if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).'.php')) 
    { 
     return $segments; 
    } 

    // Is the controller in a sub-folder? 
    if (is_dir(APPPATH.'controllers/'.$segments[0])) 
    { 
     // Set the directory and remove it from the segment array 
     $this->set_directory($segments[0]); 
     $segments = array_slice($segments, 1); 


     while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0])) 
     { 
      // Set the directory and remove it from the segment array 
      $this->set_directory($this->directory . $segments[0]); 
      $segments = array_slice($segments, 1); 
     } 

     if (count($segments) > 0) 
     { 
      // Does the requested controller exist in the sub-folder? 
      if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).'.php')) 
      { 
       if (! empty($this->routes['404_override'])) 
       { 
        $x = explode('/', $this->routes['404_override']); 

        $this->set_directory(''); 
        $this->set_class($x[0]); 
        $this->set_method(isset($x[1]) ? $x[1] : 'index'); 

        return $x; 
       } 
       else 
       { 
        show_404($this->fetch_directory().$segments[0]); 
       } 
      } 
     } 
     else 
     { 
      // Is the method being specified in the route? 
      if (strpos($this->default_controller, '/') !== FALSE) 
      { 
       $x = explode('/', $this->default_controller); 

       $this->set_class($x[0]); 
       $this->set_method($x[1]); 
      } 
      else 
      { 
       $this->set_class($this->default_controller); 
       $this->set_method('index'); 
      } 

      // Does the default controller exist in the sub-folder? 
      if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) 
      { 
       $this->directory = ''; 
       return array(); 
      } 

     } 

     return $segments; 
    } 


    // If we've gotten this far it means that the URI does not correlate to a valid 
    // controller class. We will now see if there is an override 
    if (! empty($this->routes['404_override'])) 
    { 
     $x = explode('/', $this->routes['404_override']); 

     $this->set_class($x[0]); 
     $this->set_method(isset($x[1]) ? $x[1] : 'index'); 

     return $x; 
    } 


    // Nothing else to do at this point but show a 404 
    show_404($segments[0]); 
} } 
相关问题