Php CodeIgniter-$this->;uri->;segment()返回空值
我正在尝试让这个函数正常工作,至少根据文档,我需要使用Php CodeIgniter-$this->;uri->;segment()返回空值,php,codeigniter,calendar,Php,Codeigniter,Calendar,我正在尝试让这个函数正常工作,至少根据文档,我需要使用$this->uri->segment()。日历的url如下所示: 这就意味着,据我所知,$this->uri->segment(3)是年份,$this->uri->segment(4)是月份。但是,这两个都不返回任何内容,下面是我的日历页面的代码: echo "Selected year: ".$this->uri->segment(3)." and month: ".$this->uri->segment(4);
$this->uri->segment()
。日历的url如下所示:
这就意味着,据我所知,$this->uri->segment(3
)是年份,$this->uri->segment(4)
是月份。但是,这两个都不返回任何内容,下面是我的日历页面的代码:
echo "Selected year: ".$this->uri->segment(3)." and month: ".$this->uri->segment(4);
echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4));
第一行仅用于调试目的,并确认没有任何命令返回任何输出。日历库加载到控制器中,代码如下:
public function calendar() {
// 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);
// Show page, including header and footer
$this->load->view('templates/header', $data);
$this->load->view('tasks/calendar', $data);
$this->load->view('templates/footer');
}
有人能告诉我哪里不及格吗?我一遍又一遍地阅读文档,检查代码,但我看不出我的做法与他们的示例有什么不同
更新
我想我已经排除了这个问题与$this->uri->section()函数有关。可能与控制器加载错误页面有关,当在…/tasks/calendar/(…)之后向URL添加其他节时。我还没有找到解决办法,但我想让你知道,以防其他人读到这篇文章时也有同样的问题
更新2
我认为这可能是一个路由问题,routes.php对于任何与任务相关的内容都是这样的:
$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';
要使其工作,您必须加载Codeigniter url帮助程序,因为您使用的是base\u url()
。你可以通过运行这个$this->load->helper('url')
或转到autoload.php并将其放置在帮助程序中
要使其工作,您必须加载Codeigniter url帮助程序,因为您使用的是base\u url()
。你可以通过运行这个$this->load->helper('url')
或转到autoload.php并将其放置在帮助程序中
如果您尝试echo,您会得到什么
"Selected year: ".$this->uri->segment(1)." and month: ".$this->uri->segment(2);
我很确定索引是基于routes.php
config中的default\u controller
路径设置的相对索引。如果尝试echo,会得到什么
"Selected year: ".$this->uri->segment(1)." and month: ".$this->uri->segment(2);
我很确定索引是基于routes.php
config中的default\u controller
path设置的相对索引。好的,这里的问题不是您的URI类。问题是您试图在视图中使用$this->uri->segment()
。你不应该这样做。你应该只在你的控制器内使用它。要解决这个问题,您有两个选项,可以将年和月作为参数传递给控制器方法,也可以在控制器中使用uri->段
- 示例1:将年和月作为参数传递
URL:
index.php/tasks/calendar/2015/03
控制器:
class Tasks extends MY_Controller {
public function calendar($year, $month){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $year,
"month" => $month
);
$this->load->view('tasks/calendar', $data);
}
}
class Tasks extends MY_Controller {
public function calendar(){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $this->uri->segment(3),
"month" => $this->uri->segment(4)
);
$this->load->view('tasks/calendar', $data);
}
}
视图:
- 示例2:使用
$this->uri
URL:index.php/tasks/calendar/2015/03
控制器:
class Tasks extends MY_Controller {
public function calendar($year, $month){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $year,
"month" => $month
);
$this->load->view('tasks/calendar', $data);
}
}
class Tasks extends MY_Controller {
public function calendar(){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $this->uri->segment(3),
"month" => $this->uri->segment(4)
);
$this->load->view('tasks/calendar', $data);
}
}
视图:
好的,这里的问题不是您的URI类。问题是您试图在视图中使用
$this->uri->segment()
。你不应该这样做。你应该只在你的控制器内使用它。要解决这个问题,您有两个选项,可以将年和月作为参数传递给控制器方法,也可以在控制器中使用uri->段
- 示例1:将年和月作为参数传递
URL:
index.php/tasks/calendar/2015/03
控制器:
class Tasks extends MY_Controller {
public function calendar($year, $month){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $year,
"month" => $month
);
$this->load->view('tasks/calendar', $data);
}
}
class Tasks extends MY_Controller {
public function calendar(){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $this->uri->segment(3),
"month" => $this->uri->segment(4)
);
$this->load->view('tasks/calendar', $data);
}
}
视图:
- 示例2:使用
$this->uri
URL:index.php/tasks/calendar/2015/03
控制器:
class Tasks extends MY_Controller {
public function calendar($year, $month){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $year,
"month" => $month
);
$this->load->view('tasks/calendar', $data);
}
}
class Tasks extends MY_Controller {
public function calendar(){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $this->uri->segment(3),
"month" => $this->uri->segment(4)
);
$this->load->view('tasks/calendar', $data);
}
}
视图:
我的autoload.php中有一行:$autoload['helper']=array('url')代码>,这不管用吗?还尝试在控制器中添加它(您提到的行),但没有任何运气。可能是我的路线错了吗?这应该对你有用。。那一定是别的原因。如果你这样做会得到什么:var_dump($this->uri->segment(1))代码>?返回:字符串(5)-我相信它指的是“任务”。我开始认为它实际上没有显示calendar.php页面,当url中有/[year]/[month]时……你的config.php
对base\u url
和index\u页面
?$config['index\u page']='index.php'说了什么;和$config['base_url']='';我的autoload.php中有一行代码:$autoload['helper']=array('url')代码>,这不管用吗?还尝试在控制器中添加它(您提到的行),但没有任何运气。可能是我的路线错了吗?这应该对你有用。。那一定是别的原因。如果你这样做会得到什么:var_dump($this->uri->segment(1))代码>?返回:字符串(5)-我相信它指的是“任务”。我开始认为它实际上没有显示calendar.php页面,当url中有/[year]/[month]时……你的config.php
对base\u url
和index\u页面
?$config['index\u page']='index.php'说了什么;和$config['base_url']='';然后输出为>所选年份:任务和月份:日历然后输出为>所选年份:任务和月份:日历仍然不起作用,但很可能与路线有关,但我不明白这一点(因为我从未遇到过任何问题)。在原始帖子(更新2)中添加了路线代码。请提供位于路线中的路线信息。这不是我在原始帖子“更新2”中发布的吗?还是我遗漏了什么?是的,这就是我想要的。如果你愿意,我会复习的,我会上网一会儿。我可以帮你一起调试,也许是远程调试。让我知道这仍然不起作用,但很可能与路由有关,但我不理解(因为我从来没有遇到过任何问题)。在原始帖子中添加了路线代码(更新2),请提供