2010-07-20 72 views
3

例如,在Twitter上,你可以有這樣的URL格式: http://twitter.com/username/CI基本URL路由

以「用戶名」是爲用戶的用戶名。

我想知道在Codeigniter中使用該方法的正確方法。我需要相同的格式。我有其他頁面,例如用戶帳戶管理,關於等等。我是否需要通過一個函數路由它,檢查該用戶是否存在,然後將其傳遞到另一個控制器上?謝謝!

回答

4

通過在application\libraries目錄放置MY_Router.php擴展路由器類和使用此代碼:

<?php 

class MY_Router extends CI_Router { 

    function _validate_request($segments) 
    { 
     // Does the requested controller exist in the root folder? 
     if (file_exists(APPPATH.'controllers/'.$segments[0].EXT)) 
     { 
      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().$segments[0].EXT)) 
       { 
        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.EXT)) 
       { 
        $this->directory = ''; 
        return array(); 
       } 

      } 

      return $segments; 
     } 

     // ** 
     // THIS IS THE NEW CODE BELOW 
     // ** 
     // It forces the segments to your known class (user) & method (index) 
     // for all controller calls that don't exist as files or inside 
     // directories 

     $my_segments = array('user', 'index', $segments[0]);  

     return $my_segments; 
    } 
} 

現在,只是創建一個User控制器與接受的用戶名作爲第一個參數的索引方法:

<?php 

class User extends Controller { 

    function index($username = '') 
    { 
     // Validate the HECK out of $username 
     // Validate the HECK out of $username 
     // VALIDATE THE HECK OUT OF $username 
     echo $username; 
     exit(); 
    } 

} 

這是一個ballin的答案!測試CI 1.7.2。不知道約2.0,但...

+0

笑@'//驗證閃人$ username' – BoltClock 2010-07-20 06:55:17

2

這將是很容易做這樣的事情:

http://twitter.com/u/username

如果在基本URL想它,然後別的東西,你只需創建一個名爲「U」

class U extends Controller{ 

    function index($username){ 
     echo $username; 
    } 
} 

控制器像路由等需要完成。其他人可能會讓這個CI知道如何。

3

在CI 2.0,你可以做到這一點沒有任何黑客,只是添加路由:

$route['404_override'] = 'users'; 
+0

這是否處理所有'show_404()'電話? – bschaeffer 2010-07-20 12:52:21

+0

一旦CI 2.0發佈,我就會使用它。 – 2010-07-20 15:37:21

+0

現在使用2.0,沒關係。記住ExpressionEngine,MojoMotor和PyroCMS都在使用它,所以它不會那麼不穩定。 – 2010-07-22 08:29:18

0

假設你有所謂的「作者」的控制器,功能它被稱爲「頁」,這得到作爲參數的用戶名:

class Author extends CI_Controller { 

public function page($username = null) { 
    if($username == null) { //checking for forced url page load without username specified 
     //do a 404 redirect 
    } else { 
     $this->load->model('m_users'); 
     if($this->m_users->exists($username)) { // checking if requested username exists 

      //do stuff with the user here 

     } else { //otherwise redirect 
      //do a 404 redirect 
     } 
    } 
} 

然後我會使用下面的代碼在的config/routes.php文件底部路線「your-domain.com/author/page/username「只有 「your-domain.com/username

if($handle = opendir(APPPATH.'/controllers')) { 
    while(false !== ($controller = readdir($handle))) { 
     if($controller != '.' && $controller != '..' && strstr($controller, '.') == '.php') { 
      $route[strstr($controller, '.', true)] = strstr($controller, '.', true); 
      $route[strstr($controller, '.', true).'/(:any)'] = strstr($controller, '.', true).'/$1'; 
     } 
    } 
    closedir($handle); 
} 
$route['([a-zA-Z0-9_-]+)'] = 'author/page/$1'; 

這將路線形式的任何請求your-domain.com/whateveryour-domain.com/author/page/whatever如果名稱爲「Whatever」的控制器不存在。如果存在,那麼它將訪問控制器。

在ADITION,這一切後,如果你想要做這樣的事情your-domain.com/login路由到your-domain.com/auth/login您可以通過添加以下行做您的config/routes.php文件

//... 
$route['login'] = 'auth/login'; //add this line before the one specified above 
$route['([a-zA-Z0-9_-]+)'] = 'author/page/$1';