2013-02-27 76 views
1

我想删除笨控制器名称,index.php来只显示默认的控制器方法名(连字符)

  1. 从URL从
  2. 删除index.php文件删除默认的控制器名称URL
  3. 用下划线方法名带连字符

在下面的示例fudev

  • 转换method_name是关闭根子目录,和预览是笨是在目录中。

    目前:http://devsite.com/fudev/preview/index.php/controllername/method_name

    我想有:

    http://devsite.com/fudev/preview/method-name

    任何想法?我有在配置以下/ routes.php文件来改变方法名称为连

    $route['controllername/method-name'] = 'controllername/method_name'; 
    

    ..但是当我包括controllername,我想忽略,这只是任何好处。

    任何想法?

    在此先感谢..

    乔恩。

  • +0

    很好的问题。谢谢 – 2013-02-27 13:01:10

    回答

    0

    您可以更改URL通过扩展路由器类接受连字符。 。
    中创建一个应用程序/核心称为MY_Router.php文件(假设你没有改变它在你的配置设置的默认扩展前缀

    此文件的代码;

    <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
    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 _validate_request($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); 
    
          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')) { 
            show_404($this->fetch_directory().$segments[0]); 
           } 
          } 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; 
         } 
    
         // Can't find the requested controller... 
         show_404($segments[0]); 
        } 
    } 
    

    这将重新映射所有连字符为你的控制器强调,不需要乱用htaccess。

    +0

    你需要.htaccess从url中删除index.php – 2013-02-27 12:20:19

    +2

    是的,抱歉 - 虽然在一个给定的,没有正确读取的问题。卸下在index.php是一个基本的东西,覆盖负载,无需再次重复。 – Rooneyl 2013-02-27 12:28:00