we can route in codeigniter framework, from controller or from view
- generally we route the class from controller .
public function index()
{
echo "index method";
}
this is the index function. we can call this function
- http://localhost/codi/index.php/hello or
- http://localhost/codi/index.php/hello/index
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
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