CodeIgniter - 路由问题(四个部分)

时间:2015-02-27 12:57:43

标签: php codeigniter routing calendar routes

根据我认为是$this->uri->segment()的问题,我发现它确实是一个路由问题。问题是我无法弄清楚出了什么问题,因为它看起来完全像我正在使用的另一条路线,它工作得很好,除了这个有两个可变段,而不是一个(对于正在工作的路线)。

我要显示的文件位于:

  

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

我可以使用以下命令加载它:

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

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

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

据我所知,这应该意味着像这样的链接应该有效:

  

(...)/的index.php /任务/日历/ 2015/03

然而,它没有。完整的 routes.php 如下所示:

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

我的控制器 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 的删除路由工作得很好......

2 个答案:

答案 0 :(得分:2)

要建立@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/$1/$2';

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

$today = getdate();

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

答案 1 :(得分:1)

问题是您的路线订单。这些路线:

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

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

参考:Codeigniter User Guide - URI Routing

  

注意: 路线将按照定义的顺序运行。更高的路线   将始终优先于较低的。