Thursday, June 2, 2016

how to routing in codeigniter

date:2016.06.03
we can route in codeigniter framework, from controller or from view

  • generally we route the  class from controller .
considere a php file in controller as hello.inside that class we make to three functions index,first,second with parameters $p1, $p2.index function is specific function.we can call it using class name.

public function index()
    {
        echo "index method";
    } 

this is the index function. we can call this function
  1.  http://localhost/codi/index.php/hello  or
  2. http://localhost/codi/index.php/hello/index
in both cases output output will be
index method  

other functions we create in hello.php can't call by using first url.
public function first()
    {
        echo "first method";
     }


we can call this function using http://localhost/codi/index.php/hello/first  url.
output will be
first method 


public function second($p1,$p2)
    {
        echo "second  method<br>";
        echo "my parameters are : $p1 and $p2";
    } 

  
we call the second function using http://localhost/codi/index.php/hello/second/one/two url
 output will be

second method 
my parameters are : one and two

when we calling the second function we have to give values for parameter p1 and p2.
if we don't give values or we give only one value for parameter it will show error.
we can avoid this when we defining the function we can give default values second2($p1="one",$p2="two")
if we don't give values for parameters it there will not be any errors because we already defined default value for parameters.
  http://localhost/codi/index.php/hello/second2/ten
then output will be
 second method 
my parameters are : ten and two



  • consider we created php file in view named as first.php
<h1>this is the first in view</h1>
hello ,<?php echo $name; ?>   

  
 then if we want route in view we have to create function in hello.php
public function first($value)
    {
        $data =array("name" => $value);
        $this ->load ->view('first',$data);
    }

  
here we parse the values from hello.php to first.php in view.
http://localhost/codi/index.php/hello/first/nalaka  

then output will be 

this is the first in view

hello ,nalaka 

  • we can easily get data from two or more files if use this framework
 

No comments:

Post a Comment