0

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. 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. I have 2 additional methods (lets call them methodB and methodC) in the same controller and both of them, when complete, call methodA. However, when these other methods call methodA, the url looks different. Specifically, the "objectid" variable can be either segment 6 or 7 instead of segment 5.

here's an example of what the URL looks like when methodA is called:

     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:

    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. So from methodB, I could do something like:

  $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. As soon as the ivew "allstats" loads, it shows a value inside the new parameter. Technically, it should be empty. I've double checked to make sure that only 2 methods call 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.

So far, the new code for methodA looks like this:

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

This is what my controller looks like:

public MyController extends 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);
}

}

4

1 回答 1

0

我没有 100% 关注你的问题,我不确定 URL 在调用 methodB 和 methodC 以及调用 methodA 之间是如何变化的,除非你正在做某种重定向。

如果我有这个网址

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

我有这个控制器

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();
    }
}

在 Codigniter 中,如果我有一个从这样的 url 调用的方法:

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

}

$one永远是$this->uri->segment(3),$two 永远是$this->uri->segment(4),而且$three永远是$this->uri->segment(5)

于 2012-09-25T21:21:16.037 回答