Codeigniter - 检查是否发出了GET请求

Big*_*ies 0 php codeigniter

我正在使用CodeIgniter来编写一个网站...我理解现在使用$ _GET请求就像www.website.com/function/value ..并且在控制器中获取一个url段是这样写的:

$userId = $this->uri->segment(3, 0);
Run Code Online (Sandbox Code Playgroud)

我只是想知道,当控制器加载时,我想检查是否有任何uri段,如果有则推送到一个视图,否则如果没有uri段推送到另一个.

那可能吗?

干杯.

Rep*_*pox 5

您也可以使用控制器参数.

访问/ user/profile/1时,名为User的控制器将调用方法profile()并将数字1作为方法的第一个参数传递.像这样:

class User extends CI_Controller {
{

    public function index()
    {
        $this->load->view("user_index");
    }

    public function profile ( $userId = null )
    {
        if( (int)$userId > 0 )
            $this->load->view("user_profile");
        else
            $this->load->view("another_view");
    }

}
Run Code Online (Sandbox Code Playgroud)

这是一个非常基本的样本,我只是想表明这个想法.

  • +1 - 这是CI中的正确方法.我更喜欢将检查编码为`if(is_null($ userId))...` (2认同)