usman
usman

Reputation: 55

how to get all list of controllers and methods without MY_controller and Ci_controller methods in codeigniter?

how to get all list of controllers and methods without MY_controller and Ci_controller methods in codeigniter???

Help me!!!!!!

    $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

Views: 156

Answers (2)

Jayakrishnan
Jayakrishnan

Reputation: 1335

I dont understand your question correctly . But what i guess u want to do is that , you want to get all the name of methods and classes of controllers in application/controller directory. but if these classes are inherited (extends) from MY_Controller in application/core/MY_Controller it will be also be there in the list . you want to avoid that.

if this is the case do the following:

before 2nd loop that is before line foreach( $controllers as $controller ) add the following code

include_once APPPATH . 'core/MY_Controller.php'; 
$MY_Ci_methods = get_class_methods("MY_Controller");

and before second foreach loop that is before line foreach( $methods as $method ){ add this line

$methods = array_diff($methods, $MY_Ci_methods);

hope this will clear your problem

full code:

    $this->load->helper('file');
    $controllers = get_filenames( APPPATH . 'controllers/' );

                foreach( $controllers as $k => $v )
                {
                    if( strpos( $v, '.php' ) === FALSE)
                    {
                        unset( $controllers[$k] );
                    }
                }

            echo '<ul>';

                    // add these 2 line of code.
                    include_once APPPATH . 'core/MY_Controller.php'; 
                    $MY_Ci_methods = get_class_methods("MY_Controller");

                foreach( $controllers as $controller )
                {
                    echo '<li>' . $controller . '<ul>';

                    include_once APPPATH . 'controllers/' . $controller;

                    $methods = get_class_methods( str_replace( '.php', '', $controller ) );

                    // add this line also
                    $methods = array_diff($methods, $MY_Ci_methods);

                    foreach( $methods as $method )
                    {
                        echo '<li>' . $method . '</li>';
                    }

                    echo '</ul></li>';
                }

                echo '</ul>';`

Upvotes: 1

Khant Wai Kyaw
Khant Wai Kyaw

Reputation: 138

Class not inherit from CI_Controller cannot access codeigniter function

You must use get_instance();

eg.,

Class User
{
   function index()
   {
      $CI =& get_instance();
      $CI->load->library('database');
      $CI->load->view('something');

     /*
      this above code is equal to

      $this->load->library('database');
      $this->load->view('something');
     */
   }

}

Upvotes: 0

Related Questions