简体   繁体   中英

Can't get next/previous links working for CodeIgniter 2 Calendar class

Quoting this: http://ellislab.com/codeigniter/user-guide/libraries/calendar.html

To allow your calendar to dynamically increment/decrement via the next/previous links requires that you set up your calendar code similar to this example:

$prefs = array (
    'show_next_prev'  => TRUE,
    'next_prev_url'   => 'http://example.com/index.php/calendar/show/'
);

$this->load->library('calendar', $prefs);

echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4));

You'll notice a few things about the above example:

  • You must set the "show_next_prev" to TRUE.
  • You must supply the URL to the controller containing your calendar in the "next_prev_url" preference.
  • You must supply the "year" and "month" to the calendar generating function via the URI segments where they appear (Note: The calendar class automatically adds the year/month to the base URL you provide.).

The above is the sum total of what the CodeIgniter 2 documentation says about how to generate the "next/previous" links on the calendar.


Here is how I've attempted to implement this.

In my events.php Controller file:

public function calendar()
{
    $prefs = array (
        'month_type'    => 'long',
        'day_type'      => 'short',
        'show_next_prev'=> TRUE,
        'next_prev_url' => 'http://mydomain.com/events/calendar/'
    );
    $this->load->library('calendar', $prefs);
    echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4));
}

And in my routes.php file:

$route['events/calendar'] = 'events/calendar';

When I go to /events/calendar/ , I see a calendar for the current month.

However, when I click on the next link, I get taken to /events/calendar/2013/04 as expected, but it's just a 404 page.

I think the example in the documentation is making a big assumption that the reader should automatically know what else needs to be done here. It's unclear whether the example is complete and supposed to work or if I'm supposed to know a critical piece is missing. Am I supposed to create a function called show() ? Or am I supposed to pass the URL segments back into my original calendar() function somehow and do something else with them in there? Routes? I'm not sure what.

Can anyone shed some light on this? Please be as detailed as possible. Thank-you.

You might wanna add one more rule to your route file. Something like this:

$route['events/calendar'] = 'events/calendar';
$route['events/calendar/(:any)'] = 'events/calendar';

I had two problems, both in my routing file.

1) I needed to add this route...

$route['events/calendar/(:num)/(:num)'] = 'events/calendar/$1/$2';

2) I incorrectly placed the following route above the route from #1 above, which was over-riding everthing...

$route['events/(:any)'] = 'events/view/$1';

Working...

$route['events/calendar'] = 'events/calendar';
$route['events/calendar/(:num)/(:num)'] = 'events/calendar/$1/$2';
$route['events/(:any)'] = 'events/view/$1';
$route['events'] = 'events';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM