get的方式可以直接用分段类来获取
![](https://i-blog.csdnimg.cn/blog_migrate/b4a451c4e2bed3ea624cb5f519189c5d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e2e703422a75e1cf44b4e2036862182c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/93eaf7c4c39668c6186f60792eeb57c0.png)
![](https://i-blog.csdnimg.cn/blog_migrate/95855f5aad0ec38ac2d5b60697cf91a1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e71b283b918a9d749cf41ee5826c39a5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b7e854ff8cc0c4cbb49b3b8e7920c1f5.png)
CI框架获取post和get参数_CodeIgniter心得
请参考:CI文档的输入类部分:
$this->input->post()
$this->input->get()
-----------------------------------------------------------------------------------------------------------------------
本文主要介绍在CodeIgniter框架中如何获取get和post参数。
获取get数据
在PHP主流的框架中,CI中url的pathinfo传递参数是一个特殊情况,它没有使用传统pathinfo的'c/m/key/value'
这种模式,而是在URI类中封装了segment这个方法,假设uri为/index.php/welcome/index/phpjyz/5,在控制器中调用如下
echo $this->uri->segment(3);//输出phpjyz
echo $this->uri->segment(4);//输出5
echo $this->uri->segment(1);//welcome
值得注意的是,在控制器中使用$_GET['phpjyz']是得不到5这个值的。
另外,针对get参数还可以在控制的动作(方法)加参数,例如
class Welcome extends CI_Controller {
public function index($id=0, $name=''){
echo $id.$name;
}
}
上面在index方法里加了两个参数$id和$name,有默认值表示该参数可选,uri的格式如下
index.php/welcome/index/5/phpjyz
这里传入参数的顺序不能颠倒。
获取post数据
在CI控制其中可以直接使用PHP中的$_POST['key']来获取post数据;
另外CI还封装了一个Input类,里面提供post方法来获取post提交过来的数据。
$this->input->post('key');