CodeIgniter Forums
Can I Route YYYY/MM/DD URL Segments to a Controller? - 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: Can I Route YYYY/MM/DD URL Segments to a Controller? (/showthread.php?tid=40650)



Can I Route YYYY/MM/DD URL Segments to a Controller? - El Forum - 04-14-2011

[eluser]kirkaracha[/eluser]
I'm trying to use routes to have a timeline controller handle URLs like this: example.com/2011/04/15

I tried these routes, but they aren't working (I get a 404):
Code:
$route['(:num)/(:num)/(:num)'] = 'timeline/$1/$2/$3'; //  year, month, and day
$route['(:num)/(:num)'] = 'timeline/$1/$2'; // year and month
$route['(:num)'] = 'timeline/$1/'; // year only

What am I missing?


Can I Route YYYY/MM/DD URL Segments to a Controller? - El Forum - 04-14-2011

[eluser]guidorossi[/eluser]
I think that's because the "2011" function doesn't exists under the timeline controller...

It should be:

Code:
$route['(:num)/(:num)/(:num)'] = 'timeline/foo/$1/$2/$3'; //  year, month, and day
$route['(:num)/(:num)'] = 'timeline/foo/$1/$2'; // year and month
$route['(:num)'] = 'timeline/foo/$1/'; // year only

and the controller like:

Code:
class Timeline extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function foo()
    {
      //some code...
    }
}



Can I Route YYYY/MM/DD URL Segments to a Controller? - El Forum - 04-14-2011

[eluser]kirkaracha[/eluser]
This worked, thanks!

Routes:
Code:
$route['(:num)/(:num)/(:num)'] = 'timeline/day/$1/$2/$3';
$route['(:num)/(:num)'] = 'timeline/month/$1/$2';
$route['(:num)'] = 'timeline/year/$1/';

Controller:

Code:
public function year() {
    $data = array(
        'message_text' => 'year'
    );
    $this->layouts->view('shared/display_messages',$data);
} // year

public function month() {
    $data = array(
        'message_text' => 'month'
    );
    $this->layouts->view('shared/display_messages',$data);
} // month

public function day() {
    $data = array(
        'message_text' => 'day'
    );
    $this->layouts->view('shared/display_messages',$data);
} // day