Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter route and pass values to method

Tags:

codeigniter

there is something I’m not getting about use route to call a method passing three values. I have a controller with the following method inside

public function view_day($year, $month, $day)
{
    $data['year'] = $year;
    $data['month'] = $month;
    $data['day'] = $day;

    $this->load->view('calendar/view_day', $data);
}

and a page within my views folder with the follows

<? 
    echo $this->uri->segment(5).'<p>'; 
    echo $day;
?>

finally, within my routes file I have the line below

$route['calendar/date/:num/:num/:num'] = "calendar/view_day/$1/$2/$3";

What I supposed to do is route an url like

http://www.mydomain.com/index.php/calendar/date/2012/06/10

to my calendar controller passing three values (2012, 06 and 10) to my view_day method. Then, collect these three values and pass them to my final page in order to use $day, $month and $year inside my presentation page. Now, running the url above the result is

10 (returned by the row -> echo $this->uri->segment(5).’‘;) $3 (returned by the row -> echo $day;)

Basically, what I’m not getting is way the variable $day inside my presentation page is not getting any value as passed inside the url but returns the same text ($3) I have wrote in my route statement.

Thanks

like image 334
Mario Catena Avatar asked Sep 03 '26 21:09

Mario Catena


1 Answers

You should define your route like this

$route['calendar/date/(:num)/(:num)/(:num)'] = "calendar/view_day/$1/$2/$3";
like image 184
Broncha Avatar answered Sep 05 '26 14:09

Broncha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!