将变量传递给控制器(不是函数)


Passing variable to controller (not a function)

我喜欢通过如下URL将变量传递给控制器:

a href="<?php print base_url('/profile/' . $this->session->userdata('id')); ?>">Profiel</a>

/profile/=控制器
$this->session->userdata('id')=变量

现在我想把它交给Profile,但它只在我做/Profile/index/1时工作,我喜欢这样做:/Profile/1

何时实际使用新控制器?

这是我的控制器:

<?php
class Profile extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('login');
        $this->load->helper('url');
    }
    function index()
    {
        $this->load->view('profile_view');
    }
}
?>

找到了,

我只需要将这一行添加到config文件夹中的routes.php文件中:

$route['profile/(:num)'] = "profile/index/$1"

然后,当输入profile/1时,它会自动认为它是profile/index/1

或者您可以在控制器类中使用_remap函数。阅读用户指南中的相关部分。它属于"控制器"部分。