【问题标题】:Codeigniter how to get all/specific method in current controllerCodeigniter如何在当前控制器中获取所有/特定方法
【发布时间】:2015-04-29 19:38:48
【问题描述】:

我知道这段代码:

$this->router->fetch_class(); // get current controller name
$this->router->fetch_method(); // get current method name

我想要做的是获取当前控制器或特定控制器中可用的所有方法。有人有同样的经历吗?谢谢。

解决方案

我创建助手来列出特定控制器中的所有方法

function list_this_controllers_method_except($controller, $except = array())
{
    $methods = array();

    foreach(get_class_methods($controller) as $method)
    {
        if (!in_array($method, $except))
        {
            $methods[] = $method;
        }
    }

    return $methods;
}

【问题讨论】:

  • 你在哪里叫控制器?在其他控制器中?

标签: php codeigniter class methods controller


【解决方案1】:

你可以使用原生 php 来获取类方法

get_class_methods($this);

$this 是被调用的控制器

仅供参考

class user extends CI_Controller {

    public function __construct() {

                #-------------------------------
                # constructor
                #-------------------------------

                parent::__construct();

                var_dump(get_class_methods($this));

            }

}

阅读文档

http://php.net/manual/en/function.get-class-methods.php

【讨论】:

  • 太好了,我使用了这个php函数,但是如何不打印像construct和get_instance这样的特定函数?
  • 我认为你不能......也许在返回结果中你可以过滤掉你想要的不必要的功能
【解决方案2】:

-Oli Soproni B.(https://stackoverflow.com/users/1944946/oli-soproni-b)

感谢我已经这样使用它了:

class user extends CI_Controller {

    public function __construct() {

                #-------------------------------
                # constructor
                #-------------------------------

                parent::__construct();

                $methodList = get_class_methods($this);
                foreach ($methodList as $key => $value) {
                 if($value!="__construct"&&$value!="get_instance"&&$value!="index") {
            echo "$value"."\n";
                 }

                }

            }

}

junior

【讨论】:

    【解决方案3】:

    在控制器中试试这个测试功能

        public function test()
        {   
         $this->load->helper('file');
         $controllers = get_filenames( APPPATH . 'controllers/' ); 
    
        foreach( $controllers as $k => $v )
        {
            if( strpos( $v, '.php' ) === FALSE)
            {
                unset( $controllers[$k] );
            }
        }
    
        echo '<ul>';
    
        foreach( $controllers as $controller )
        {
            echo '<li>' . $controller . '<ul>';
    
            include_once APPPATH . 'controllers/' . $controller;
    
            $methods = get_class_methods( str_replace( '.php', '', $controller ) );
    
            foreach( $methods as $method )
            {
                echo '<li>' . $method . '</li>';
            }
    
            echo '</ul></li>';
        }
    
        echo '</ul>'; 
    } 
    

    【讨论】:

      猜你喜欢
      • 2015-08-01
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多