![]() |
Question about route and pass values to method - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Question about route and pass values to method (/showthread.php?tid=49576) |
Question about route and pass values to method - El Forum - 02-25-2012 [eluser]skunkio[/eluser] Hi all, 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 Code: public function view_day($year, $month, $day) and a page within my views folder with the follows Code: <? finally, within my routes file I have the line below Code: $route['calendar/date/:num/:num/:num'] = "calendar/view_day/$1/$2/$3"; What I supposed to do is route an url like Code: 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).'<p>' ![]() $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 Question about route and pass values to method - El Forum - 02-25-2012 [eluser]CroNiX[/eluser] Does it work if you write :num as (:num) ? Question about route and pass values to method - El Forum - 02-25-2012 [eluser]skunkio[/eluser] Using (:num) now it's working. So, do I have to use (:num - :any) in my routes file everytime I have more than one value define in my method's signature, otherwise I can just use :num - :any? Thanks Question about route and pass values to method - El Forum - 02-26-2012 [eluser]InsiteFX[/eluser] Yes because they define the parameters being passed and how many! Question about route and pass values to method - El Forum - 02-26-2012 [eluser]CroNiX[/eluser] Should always use (:num) and (:any) as that is what the regex is searching for. I realize they use :num in the example in the userguide; it's wrong. Question about route and pass values to method - El Forum - 02-27-2012 [eluser]Aken[/eluser] Using parentheses in your routes creates something called a back reference - the $1, $2, etc... are back references to those specific parts of the matched route. If you use :num, :any, or a particular regular expression without the parentheses properly, there will be no reference to that part of the URL. |