[英]codeigniter uri segment not working
我有一个Ajax函数-
$(document).ready(function(){
$.ajax({
url: base_url+'movies/index/this_week/hindi',
type: "POST",
success: function( data )
{
$("#news1").html(data);
}
});
});
在我的电影控制器中,当我在_remap()函数中打印$this->uri->segment(2)
或$this->uri->segment(3)
,它总是返回随机值。 有时它返回两个语句的索引,或者有时它返回“ index”和“ hindi”等。
每当我刷新页面时,它都会返回随机值。 我很困惑。 为什么会发生?
这是_remap()函数。
function _remap()
{
$segment_1 = $this->uri->segment(1);
echo "1==>".$this->uri->segment(1);
echo " 2==>".$this->uri->segment(2);
echo " 3==>".$this->uri->segment(3);
echo " 4==>".$this->uri->segment(4);
switch ($segment_1) {
case null:
case false:
case '':
$this->index();
break;
case 'movies':
if($this->uri->segment(2) == "index" && $this->uri->segment(3) == "this_week")
{
$this->moviedescription($this->uri->segment(4));
}
else
{
$this->moviedescription();
}
break;
default:
$this->index();
break;
}
}
任何帮助表示赞赏。 提前致谢。
比使用uri段更好的方法。 尝试使用函数参数,如下所示:
您的movies
控制器:
public function index($when = false, $type = false) {
...
echo $when, ', ', $type;
// $when should be this_week and $type should be hindi, in your example)
// $when isn't the best variable name, but you get the idea
}
然后,通常将这些参数作为变量访问,因为它们将始终相同,没有随机值。 我已将其默认值设置为false,以防万一您一直不使用它们(例如,如果您具有默认的movies
页面)。
它不应该为您提供随机值,这真的很奇怪,现在不确定。
当您向控制器发出ajax请求(从示例中)时,正在接收的控制器应具有:
echo $this->uri->segment(1); // result "movies"
echo $this->uri->segment(2); // result "index"
echo $this->uri->segment(3); // result "this_week"
echo $this->uri->segment(4); // result "hindi"
您可以传递第二个参数作为默认值。
var_dump($this->uri->segment(5,FALSE)); // result BOOL FALSE
我已经在计算机上检查了您的代码,它可以正常运行。 因此,如果在JAVASCRIPT中正确定义了base_url
,问题就不在代码中。 仔细检查一下。
您可能在路由配置文件中设置了其他路由,而该配置文件与url混淆了,或者.htaccess
文件有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.