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';