【问题标题】:Codeigniter get controller name in helperCodeigniter 在帮助器中获取控制器名称
【发布时间】:2017-11-25 04:32:33
【问题描述】:

我有一个用于记录的自定义助手。

在助手的其中一项功能中,我需要获取被调用的控制器的名称。有没有办法做到这一点?

我不能依赖 uri 段,因为某些控制器位于子文件夹中,并且整个都使用了助手。

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    您可以在 CI2.x 中使用以下内容

    $this->router->fetch_class();
    

    在这种情况下,您可能需要先获取 CI 超级变量 $this 的实例。使用以下内容:

    $ci =& get_instance();
    $ci->router->fetch_class();
    

    还有一个$ci->router->fetch_method(); 方法,如果您出于任何原因需要调用方法的名称。

    【讨论】:

      【解决方案2】:

      如果您执行以下操作,$this->>router->fetch_method(); 将返回 index

      class Someclass extends CI_Controller {        
          function index(){        
              $this->edit();        
          }        
          function edit(){        
              $this->router->fetch_method(); //outputs index
          }
      }
      

      【讨论】:

        【解决方案3】:

        这应该可以工作(不太确定它是否在帮助程序中工作):

        $ci =& get_instance();
        $ci->router->class // gets class name (controller)
        $ci->router->method // gets function name (controller function)
        

        【讨论】:

          【解决方案4】:

          你也可以使用URI类

          $ci = & get_instance();
          $ci->uri->segment(1) // That stands for controller
          $ci->uri->segment(2) // That stands for method
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-11-01
            • 2012-08-11
            • 2012-02-06
            • 1970-01-01
            • 2010-12-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多