简体   繁体   English

codeigniter方法显示uri段值,而不是空字符串或null

[英]codeigniter method showing a uri segment value instead of empty string or null

I have a weird problem. 我有一个奇怪的问题。 Hopefully, there's a simple explanation / a simple bug somewhere in my code. 希望我的代码中有一个简单的解释/一个简单的错误。

I have methodA in my controller, that needs 3 parameters from the url. 我的控制器中有methodA,它需要url中的3个参数。 The code looks like this: 代码如下:

      $data['listofpts'] = $this->sw_model->get_all_widgets($this->uri->segment(3),$this->uri->segment(4) );         
      $data['model'] = $this->uri->segment(4);
      $data['ip'] = $this->uri->segment(3); 
      $data['objectid'] = $this->uri->segment(5);         

      $data['main_content']='allstats';
      $this->load->view('includes/template', $data); 

You can see that I'm grabbing 3 pieces of from the url. 您可以看到我从URL中获取了3个。 I have 2 additional methods (lets call them methodB and methodC) in the same controller and both of them, when complete, call methodA. 我在同一个控制器中有2个其他方法(让它们分别称为methodB和methodC),并且在完成时都调用methodA。 However, when these other methods call methodA, the url looks different. 但是,当这些其他方法调用methodA时,URL看起来有所不同。 Specifically, the "objectid" variable can be either segment 6 or 7 instead of segment 5. 具体来说,“ objectid”变量可以是段6或7,而不是段5。

here's an example of what the URL looks like when methodA is called: 这是调用methodA时URL的示例:

     http://myserver/myapp/mycontroller/methodA/10.14.123.123/H8699A/417

Here's an example of what the URL looks like when methodB is called: 这是调用methodB时URL的示例:

    http://myserver/myapp/mycontroller/methodB/10.14.3.44/H8699A/A14/417

I'm not sure if this is good design or not, but I decided to change the signature of methodA so that it accepts a number, representing the segment ID. 我不确定这是否是一个好的设计,但是我决定更改methodA的签名,以便它接受一个代表段ID的数字。 So from methodB, I could do something like: 因此,从methodB可以执行以下操作:

  $this->methodA(6);

PROBLEM / QUESTION: There are two things that i don't understand. 问题/问题:我不了解两件事。 The first question is why the new parameter is displaying a value when you just call methodA on its own. 第一个问题是,当您自己单独调用methodA时,为什么新参数显示一个值。 As soon as the ivew "allstats" loads, it shows a value inside the new parameter. 只要加载了ivew“ allstats”,它就会在新参数中显示一个值。 Technically, it should be empty. 从技术上讲,它应该为空。 I've double checked to make sure that only 2 methods call methodA. 我已仔细检查以确保只有2个方法调用methodA。

Second, I don't understand why when i dump the contents of the parameter, its showing the IP address, which is uri segment 3. 其次,我不明白为什么我转储参数的内容时会显示IP地址,即URI段3。

So far, the new code for methodA looks like this: 到目前为止,methodA的新代码如下所示:

public function methodA($uriObjectid)
{
 echo $uriObjectid;
      $data['listofpts'] =   $this->switches_model->get_all_widgets($this->uri->segment(3),$this->uri->segment(4) );         
      $data['model'] = $this->uri->segment(4);
      $data['ip'] = $this->uri->segment(3); 
      $data['objectid'] = $this->uri->segment(5); 


      $data['main_content']='allstats';
      $this->load->view('includes/template', $data);

}//end methodA

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

EDIT 1 编辑1

This is what my controller looks like: 这是我的控制器的外观:

public MyController extends CI_Controller { 公共MyController扩展CI_Controller {

public methodA($optionalparm) 
{
    //url looks like: http://myserver/myapp/thiscontroller/value1/valueformethodA

   echo $optionalparm;  //this is the variable that's problematic. it should be 3 or 4.

   $valueformethodA = $this->uri->segment(2)
}

public methodB() {
    //url looks like: http://myserver/myapp/thiscontroller/value1/value2/value3/valueformethodA
    //call methodA
    // $valueformethodA is now uri segment 4.  pass that as argument
    $this->methodA(4);
}

public methodC() {

    //url looks like: http://myserver/myapp/thiscontroller/value1/value2/valueformethodA
    //call methodA
   // $valueformethodA is now uri segment 4.  pass that as argument
    $this->methodA(3);
}

} }

I'm not following your question 100%, and i'm unsure how the url is changing in between calling methodB and methodC and calling methodA, unless you're doing some sort of redirect. 我不是100%关注您的问题,并且我不确定在调用methodB和methodC以及调用methodA之间url会如何变化,除非您要进行某种重定向。

If i have this url 如果我有这个网址

http://example.com/myController/methodB/segment3/segment4/segment5/segment6

And i have this controller 我有这个控制器

public MyController extends CI_Controller {

    public methodA() {
        // the url should still be the same in here as it was in methodB unless a redirect was called.
    }

    public methodB() {
        // call methodC
        $this->methodC();
    }

    public methodC() {
        // call methodA
        $this->methodA();
    }
}

In Codigniter, if i have a method that is called from a url like this: 在Codigniter中,如果我有一个从url这样调用的方法:

public testMethod($one, $two, $three) {

}

$one is always going to be $this->uri->segment(3) , $two is always going to be $this->uri->segment(4) , and $three is always going to be $this->uri->segment(5) ; $one始终是$this->uri->segment(3) ,$ two始终是$this->uri->segment(4)$three始终将是$this->uri->segment(5) ;

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

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