简体   繁体   中英

How does codeigniter know how to pass parameters from controller to the model

I am starting to learn codeigniters active record and i am querying my database using parameters passed from the controller to the model.

First i am passing the id from the controller to the model and that works.

Controller

function bret($id){
$this->load->model('school_model');
$data = $this->school_model->get_city_and_population($id);
foreach ($data as $row)
{
echo "<b>Name Of The City</b>...........". $row['Name'];
echo "<br/>";
echo "<b>Total Population</b>...........".$row['Population'];
}
}

Model

function get_city_and_population($id){
$this->db->select('Name,Population');
$query = $this->db->get_where('city', array('ID'=>$id));
return $query->result_array();
}

I went ahead and put in multiple parameters expecting to fail but this works but i am not so sure why it worked or what worked.

Controller

public function parameters($id,$name,$district){
    $this->load->model('school_model');
    $data = $this->school_model->multiple_parameters($id,$name,$district);
    foreach ($data as $row)
    {
    echo "<b>Total Population</b>...........".$row['Population'];
    }
    }

Model

function multiple_parameters($id,$name,$district){
$this->db->select('Population');
$query = $this->db->get_where('city', array('ID'=>$id,'Name'=>$name,'District'=>$district));
return $query->result_array();
}

In my multiple parameters example i visited http://example.com/env/at/index.php/frontpage/parameters/7/Haag/Zuid-Holland/

Here,i know the name Haag is in id 7 and the district is Zuid-Holland

Here are my questions.How does codeigniter know how to pass the parameters from the url to the model and secondly,what if i was slightly wrong like 7/Haag/Zuid-Hollandes/ ,how would i show the user that,that url is wrong and fallback to a default value instead of showing blank when the parameters are wrong?.

//In codeiginter URI contains more then two segments they will be passed to your function as parameters.
//if Url: http://example.com/env/at/index.php/frontpage/parameters/7/Haag/Zuid-Holland/

//Controller: forntpage
public function parameters($id,$name,$district){
   echo $id.'-'$name.'-'.$district;
}

//and if you are manually getting url from segment & want to set default value instead of blank then use following:



public function parameters(
$this->load->helper("helper");
$variable=$this->uri->segment(segment_no,default value);
//$id=$this->uri->segment(3,0);
}

//or
 //Controller: forntpage
 public function parameters($id='defaultvalue',$name='defaultvalue',$district='defaultvalue'){
   echo $id.'-'$name.'-'.$district;
}

That's just simple uri mapping in CI, or uri param binding if you will.
When you have a method like:

public function something($param1, $param2) {
    // get from: controller/something/first-param/second-param
}

That means your uri segments are passed as arguments to your controller method.

The above method could be written as:

public function something() {
    $param1 = $this->uri->segment(3);
    $param2 = $this->uri->segment(4);
    // segment 1 is the controller, segment 2 is the action/method.
}

You need to understand that you have to manually check if the uri segments are exactly what you want them to be, as CI doesn't do anything else than this mapping.

Next, if you want to have some defaults, following statement is true:

public function something($param1 = 'some default value', $param2 = 'other value') {
// get from: controller/something/first-param/second-param
}

That is, if a url like: /controller/something is passed along, you will still get your default values back. When controller/something/test is passed, your first param is overridden by the one from the url (test).

That's pretty much it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM