CodeIgniter - 路由问题(四段) [英] CodeIgniter - Routing issues (four segments)

查看:117
本文介绍了CodeIgniter - 路由问题(四段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照我认为的问题, $ this-> uri-> segment (),我发现它实际上是一个路由问题。问题是,我不能真正弄清楚什么是错误,因为它看起来像另一条路线,我使用,它工作正常,除了这一个有两个变量段,而不是一个(对于正在工作的路由)。

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).

我要显示的档案位于:


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

[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

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

但是,它不是。完整的 routes.php 如下所示:

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

我的控制器 tasks.php p>

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

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

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...

推荐答案

@codeGodie刚才说,你需要稍微重新排序你的路由定义。

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

您可能还需要将$ year = null,$ month = null设置为有效值。 p>

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']
}

这篇关于CodeIgniter - 路由问题(四段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆