2013-02-27 48 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