【问题标题】:codeigniter method showing a uri segment value instead of empty string or null显示 uri 段值而不是空字符串或 null 的 codeigniter 方法
【发布时间】:2012-09-28 21:21:37
【问题描述】:

我有一个奇怪的问题。希望在我的代码中某处有一个简单的解释/一个简单的错误。

我的控制器中有 methodA,它需要来自 url 的 3 个参数。代码如下所示:

      $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); 

你可以看到我从 url 中抓取了 3 个片段。 我在同一个控制器中有 2 个额外的方法(我们称它们为 methodB 和 methodC),并且它们都在完成后调用 methodA。但是,当这些其他方法调用 methodA 时,url 看起来会有所不同。具体来说,“objectid”变量可以是段 6 或 7,而不是段 5。

以下是调用 methodA 时 URL 的示例:

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

以下是调用 methodB 时 URL 的示例:

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

我不确定这是否是一个好的设计,但我决定更改 methodA 的签名,以便它接受一个代表段 ID 的数字。 所以从方法B,我可以做类似的事情:

  $this->methodA(6);

问题/问题:有两件事我不明白。第一个问题是为什么当你自己调用 methodA 时新参数会显示一个值。一旦加载了 ivew “allstats”,它就会在新参数中显示一个值。从技术上讲,它应该是空的。我已经仔细检查以确保只有 2 个方法调用 methodA。

其次,我不明白为什么当我转储参数的内容时,它显示的IP地址是uri段3。

到目前为止,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

任何帮助将不胜感激。 谢谢。

编辑 1

这是我的控制器的样子:

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

}

【问题讨论】:

  • 你首先从你的url调用什么方法? MethodB 就像我在示例中所做的那样?
  • 没有@Catfish。第一个被调用的方法是 MethodA,但没有传入任何参数。
  • 我显然不明白你想要做什么或问题是什么。 $optionalparm 总是与$this->uri->segment(3) 相同;在 methodB 和 methodC 中,如果你试图传递一个段,你需要做类似$this->methodA($this->uri->segment(4));

标签: php codeigniter


【解决方案1】:

我没有 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    相关资源
    最近更新 更多