在codeIgniter中管理controlller的方法参数

时间:2013-09-09 08:48:42

标签: php codeigniter codeigniter-url codeigniter-routing

这是我的方法

public function sale($id,$type){
   if($id==TRUE){
       .....................
       .....................
    }
    if($type==TRUE){
      .......................
      .......................
    }
}

我想单独浏览两个参数,假设

  

http://mysite.com/mycontroller/sale/id

  

http://mysite.com/mycontroller/sale/type

怎么可能

2 个答案:

答案 0 :(得分:1)

试试这个:
网址格式:http://mysite.com/mycontroller/sale/id(不可能)
现在:http://mysite.com/mycontroller/sale/selector/id/value/1
现在:http://mysite.com/mycontroller/sale/selector/type/value/1

function sale(){
   $type    = $this->uri->segment(4);  #get the selector
   $val     = $this->uri->segment(6);  #get the value
   if( $type == "id" ){
        #in id selection
   }else if( $type == "type" ){
      #in type selection
   }else{
        redirect('somewhere', 'refresh');
   }
}

答案 1 :(得分:0)

您可以使用网址传递多参数:

例如关注网址:

http://mysite.com/mycontroller/sale

http://mysite.com/mycontroller/sale/3

http://mysite.com/mycontroller/sale/3/computer

此处代码:

public function sale($id = false, $type = false) {

   if($id) {
       // something to do
   }

   if($type) {
      // something to do
   }
}