user1765862
user1765862

Reputation: 14185

codeigniter how to call function which resides in another controller

One stupid question, I want to call from my admin_news controller function which resides in another controller Admin. FUnction name is is_logged_in(); admin.php

public function is_logged_in()
{
   ....
}


admin_news.php
public function __contruct()
{
   parent::__construct();
   //admin->is_logged_in();??
}

how can I do that? Thanks

Upvotes: 0

Views: 8937

Answers (1)

complex857
complex857

Reputation: 20753

You will have to move that functionality somewhere else, Codeigniter's architecture doesn't allow multiple controller instances in one request. You have multiple options like using a common base class, libraries, helpers and so on.

I would recommend you to create your own MY_Controller base class (see Extending Core Classes) and put your method there, like this:

class MY_Controller extends CI_Controller {
    protected function is_logged_in() {
        // ...
    }  
}

Once you have it there you can call it like:

class AdminNews extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->is_logged_in();
    }
}

Upvotes: 6

Related Questions