简体   繁体   English

Codeigniter URI段不起作用

[英]codeigniter uri segment not working

I have a Ajax function - 我有一个Ajax函数-

$(document).ready(function(){
$.ajax({
        url: base_url+'movies/index/this_week/hindi',
        type: "POST",
        success: function( data )
        {               
        $("#news1").html(data);
        }                 
     });
});

In my movies controller, when i print $this->uri->segment(2) or $this->uri->segment(3) in _remap() function it always return random value. 在我的电影控制器中,当我在_remap()函数中打印$this->uri->segment(2)$this->uri->segment(3) ,它总是返回随机值。 Sometimes it returns index for both statements or sometimes it returns 'index' and 'hindi' etc. 有时它返回两个语句的索引,或者有时它返回“ index”和“ hindi”等。

All the time when i refresh the page, it returns random value. 每当我刷新页面时,它都会返回随机值。 I am getting confused. 我很困惑。 Why it occurs? 为什么会发生?

Here is _remap() function. 这是_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;
            }
        }

Any help appreciated. 任何帮助表示赞赏。 Thanks in advance. 提前致谢。

There's probably a better approach than using uri segments. 比使用uri段更好的方法。 Try using function parameters, like this: 尝试使用函数参数,如下所示:

Your movies controller: 您的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
}

And then just access those parameters normally as variables as they will always be the same, no random values. 然后,通常将这些参数作为变量访问,因为它们将始终相同,没有随机值。 I've set their default values to false just in case you don't use them all the time (for example, if you have the default movies page). 我已将其默认值设置为false,以防万一您一直不使用它们(例如,如果您具有默认的movies页面)。

it should not give you random values, that realy wierd and don't now for sure. 它不应该为您提供随机值,这真的很奇怪,现在不确定。

when you make your ajax request (from your example) to the controller, the reveiving controller should have: 当您向控制器发出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"

you could pass an second param for the default value. 您可以传递第二个参数作为默认值。

var_dump($this->uri->segment(5,FALSE)); // result BOOL FALSE

I have checked your code in my computer and it works perfectly. 我已经在计算机上检查了您的代码,它可以正常运行。 So, the problem is not in the code if base_url is defined correctly in JAVASCRIPT. 因此,如果在JAVASCRIPT中正确定义了base_url ,问题就不在代码中。 Double check that. 仔细检查一下。

You may have set other routes in routes config file which is messing with the url or you may have problem with .htaccess file. 您可能在路由配置文件中设置了其他路由,而该配置文件与url混淆了,或者.htaccess文件有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM