【问题标题】:CodeIgniter - Routing issues (four segments)CodeIgniter - 路由问题(四段)
【发布时间】:2015-02-27 12:57:42
【问题描述】:

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

我要显示的文件位于:

[主文件夹]/application/views/tasks/calendar/calendar.php

我可以用命令加载它:

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

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

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

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

(...)/index.php/tasks/calendar/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 的删除路线工作得很好......

【问题讨论】:

  • 您的大多数路线都像 1 = 1 。像这样设置它们不会做任何事情。
  • 想详细说明一下?我所有的其他路线都在工作,如果它们没有功能,那是怎么回事?
  • 他们当然会工作,因为他们什么都不做。让我说明一下:$route['auth'] = 'auth'; 表示将段中包含 auth 的任何 URI 重定向到 auth 类,即 .tld/auth 重定向到 .tld/auth。首先我知道你的问题是什么,但我不认为路线是原因。
  • "路由将按照它们定义的顺序运行。高路由总是优先于低路由。"尝试将“tasks/calendar/(:num)/(:num)”移到顶部。
  • 感谢克雷格,如果我把它放在列表的第一行,它确实有效。但是,我不明白为什么,因为它已经是“任务”的第一条路线。它仍然选择任务的索引页吗?

标签: php codeigniter routing calendar routes


【解决方案1】:

要建立在 @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']
}

【讨论】:

  • 感谢所有评论的人,问题确实是路由定义的顺序,而不是定义本身。所以这是其他 CI 新手的教训,比如我。再次感谢大家...
【解决方案2】:

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

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

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

参考:Codeigniter User Guide - URI Routing

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

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多