Monday, June 20, 2016

Routing in model

Routing in  model

Here we rout files in model folder.1
First we create New_model .php file in model.
<?php
 class New_model extends CI_Model{
public function getProfile($name)
                {
                                return array("fulname"=>"jack sparrow","age"=>40);
                }
 }
Here we create array and return it when the getProfile method is calling.
In controller we create hello.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hello extends CI_Controller
{
                public function first($value)
                {
                                $this ->load->model("new_model");
                                $profile =$this->new_model->getProfile("jack");
                                print_r($profile);
                }
}
?>
We can route new_model.php as above mentioned.
We should use following url.
then the output is Array ( [fulname] => jack sparrow [age] => 40 )
if we want to use following url
http://localhost/codi/index.php/hello/first we have to define method as follows.
public function first()
then output as same
create first.php in views
<h1>this is the first in view</h1>
<fieldset>
<legend>detail</legend>
hello ,<?php echo $profile["fulname"];
echo "<br>";
echo "age is ".$profile["age"];

 ?></fieldset>
                Change function first in hello.php as follows.
public function first($value)
                {
                                $this ->load->model("new_model");
                                $profile =$this->new_model->getProfile("jack");
                                $data['profile']=$profile;
                                $this -> load ->view('first',$data);
                }
then output is follow.




No comments:

Post a Comment