2014-01-30 86 views
-2

我需要在CodeIgniter中像Facebook一样重定向我的URL。例如:假设我们给'example.com/username',那么它将显示用户配置文件信息。 但我的控制器是'example.com/index/username'。这里的用户名是参数。 这里是我的控制器如何重新映射CodeIgniter中的URL

class Index extends CI_Controller { 
public function __construct() { 
      parent::__construct(); 

     } 

public function index($username=NULL) { 

    $this->load->view('profile',$data); 

} 

}

请帮助我。

+0

发生了什么事,当你阅读手册,并试图它在那里说的吗? – stormdrain

+0

example.com/index/testusername.....这是我的url ... index是我的控制器名称,testusername是参数。我需要将其更改为example.com/testusername。 – Meera

+1

噢,在这种情况下,当您阅读手册并尝试其中的内容时发生了什么? – stormdrain

回答

0
$route['404_override'] = 'profile'; 

class Index extends CI_Controller { 
public function __construct() { 
     parent::__construct(); 
} 

public function index() 
{ 
    $username = $this->uri_segment(1); 

    if (empty($username)) { 
     $this->displayPageNotFound(); 
    } 

    // Check if parameter is not a valid username. 
    if (!$this->checkIfUsername($username)) { 
     $this->displayPageNotFound(); 
    } else { 
     dosomething(); 
    } 

}}