CodeIgniter - 路由问题(四段)

CodeIgniter - Routing issues (four segments)

根据我认为 $this->uri->segment() 的问题,我发现这实际上是一个路由问题。问题是我无法真正找出问题所在,因为它看起来与我正在使用的另一条路线完全一样,它工作正常,除了这条路线有两个可变段,而不是一个(对于正在工作的路线)。

我要显示的文件位于:

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

我可以用命令加载它:

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

但是,当我想将当前年份和月份作为最后两个段传递时,它不起作用:

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

据我所知,这应该意味着像这样的 link 应该可以工作:

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

然而,事实并非如此。完整的 routes.php 如下所示:

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

我的控制器,tasks.php,看起来像这样:

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');
}

非常简单的视图文件,calendar.php,看起来像这样:

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

我到底做错了什么? projects 的删除路由工作正常...

问题出在您的路线顺序上。这些路线:

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

应该在您的路线列表的最底部,否则它们将在您的任务路线之前看到。

参考:Codeigniter User Guide - URI Routing

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

要以@Craig 和@CodeGodie 刚才所说的为基础,您需要稍微重新排序路由定义。

$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//';

您可能还想将 $year=null、$month=null 设置为有效值。

$today = getdate();

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