[英]CodeIgniter - Routing issues (four segments)
按照我认为是$ 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';
我的控制器task.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);
我到底在做什么错? 项目的删除路线可以正常工作...
要以@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']
}
问题是您的路线顺序。 这些路线:
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view'
应该在您的路线列表的最底部,否则它们将在您的任务路线之前显示。
注意: 路由将按照定义的顺序运行。 较高的路线将始终优先于较低的路线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.