我是codeigniter的新手,我有一个问题 是否可以使用相同的Url模式在Controller上访问2种不同的方法? 不同的是访问这些方法的方法是GET或POST,就像Spring MVC处理它一样
@RequestMapping(value = "/persons/add", method = RequestMethod.GET)
public String getAdd(Model model)
@RequestMapping(value = "/persons/add", method = RequestMethod.POST)
public String add(@ModelAttribute("personAttribute") Person person)
我想要的是,当我访问这样的网址www.bla.com/controller/AddFunction(这是GET)时,将触发的方法是“add1”方法,它将加载视图表单然后我有对控制器/ AddFunction有“动作”的表单,此时因为它是POST它会触发“add2”方法
事先感谢
答案 0 :(得分:2)
您不需要在函数调用中传递REST方法的名称。控制器本身可以处理来自GET,POST或两者的输入。
示例:
class Persons extends Controller{
function add(){
//$p will contain post data.
$p = $this->input->post();
//$g will contain get data.
$g = $this->input->get();
//$b will contain get or post data, depending on which is submitted.
$b = $this->input->get_post();
}
}
有关进一步说明,请参阅CodeIgniter关于The Input Class
的文档答案 1 :(得分:0)
class Persons extends Controller{
function add(){
$postArray = $this->input->post();
$getArray = $this->input->get();
//if something was POSTed
if($postArray){
//do something with post array
return; //early return
}
//if we need to GET
if($getArray){
//do something with get array
return; //early return
}
//do something down here if there were no data passed, like a default view
//get() and post() return false if you have no parameters sent
}
}