junior
junior

Reputation: 808

Codeigniter how to get all/specific method in current controller

I know this code:

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

What I want to do is get all the method available in current controller or specific controller. Anyone had the same experience? Thank You.

SOLUTIONS

I create helper to list all method in the specific controller

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

Upvotes: 5

Views: 3672

Answers (3)

Vinoth Smart
Vinoth Smart

Reputation: 393

Try This test function in controller

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

Upvotes: 0

Rohit Dhiman
Rohit Dhiman

Reputation: 51

-Oli Soproni B.(https://stackoverflow.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

Upvotes: 1

Oli Soproni B.
Oli Soproni B.

Reputation: 2800

you can use native php to get the class methods

get_class_methods($this);

the $this is the controller being called

Sample only

class user extends CI_Controller {

    public function __construct() {

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

                parent::__construct();

                var_dump(get_class_methods($this));

            }

}

read on the documentation

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

Upvotes: 4

Related Questions