Codeigniter 如何在当前控制器中获取 all/specific 方法

Codeigniter how to get all/specific method in current controller

我知道这个代码:

$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 获取 class 方法

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

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

Thanks I have used it like this:

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

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

    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>'; 
}