Welcome Guest, Not a member yet? Register   Sign In
Question about route and pass values to method
#1

[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)
    {
        $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

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

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>'Wink
$3 (returned by the row -> echo $dayWink

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
#2

[eluser]CroNiX[/eluser]
Does it work if you write :num as (:num) ?
#3

[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
#4

[eluser]InsiteFX[/eluser]
Yes because they define the parameters being passed and how many!
#5

[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.
#6

[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.




Theme © iAndrew 2016 - Forum software by © MyBB