简体   繁体   中英

CodeIgniter - Routing issues (four segments)

Following what I thought was an issue with $this->uri->segment() , I figured out that it is actually a routing issue. Problem is that I can't really figure out what is wrong, as it looks exactly like another route I am using, which works fine, except this one has two variable segments, rather than one (for the route that is working).

The file I am trying to show is located in:

[main folder]/application/views/tasks/calendar/calendar.php

And I can load it with the command:

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

However, when I want to pass the current year and month as the last two segments, it does not work:

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

To my knowledge this should mean that a link like this should work:

(...)/index.php/tasks/calendar/2015/03

However, it does not. The full routes.php looks like this:

$route['auth/(:any)']                   = 'auth/view/$1';
$route['auth']                          = 'auth';
$route['projects/delete/(:any)']        = 'projects/delete/$1';
$route['projects/create']               = 'projects/create';
$route['projects/(:any)']               = 'projects/view/$1';
$route['projects']                      = 'projects';
$route['(:any)']                        = 'pages/view/$1';
$route['default_controller']            = 'pages/view';
$route['category']                      = 'category';
$route['category/(:any)']               = 'category/view/$1';
$route['tasks/create']                  = 'tasks/create';
$route['tasks/calendar/(:num)/(:num)']  = 'tasks/calendar/$1/$2';
$route['tasks/calendar']                = 'tasks/calendar';
$route['tasks']                         = 'tasks';
$route['tasks/(:any)']                  = 'tasks/view/$1';

And my controller, tasks.php , looks like this:

public function calendar($year = null, $month = null) {
    // Calender configuration (must be done prior to loading the library
    $conf = array(
            'start_day' => 'monday',
            'show_next_prev' => true,
            'next_prev_url' => base_url() . 'index.php/tasks/calendar'
    );

    // Load libraries and helpers
    $this->load->library('calendar',$conf);

    // Set variables for $data array
    $data['year']  = $year;
    $data['month'] = $month;

    // Show page, including header and footer
    $this->load->view('templates/header', $data);
    $this->load->view('tasks/calendar', $data);
    $this->load->view('templates/footer');
}

And the very simple view file, calendar.php , looks like this:

<?php
echo "Selected year: ".$year." and month: ".$month;
echo $this->calendar->generate($year, $month);

What the heck am I doing wrong? The delete routes for projects works just fine...

To build on what @Craig and @CodeGodie have just said, you need to re-order your route definitions slightly.

$route['tasks']                         = 'tasks/index';

// Initial route that will use $year=null, $month=null
$route['tasks/calendar']                = 'tasks/calendar';

// This route will use whatever $year, $month the user provides
$route['tasks/calendar/(:num)/(:num)']  = 'tasks/calendar/$1/$2';

You may also want to set $year=null, $month=null to valid values.

$today = getdate();

if(is_null($year) || is_null($month)){
    $year  = $today['year'];
    $month = $today['month']
}

The problem are your routes order. These routes:

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

should be at the very bottom of your routes list or else they will be seen prior your tasks routes.

Reference: Codeigniter User Guide - URI Routing

Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

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