Organizational Research By

Surprising Reserch Topic

codeigniter extending multiple controllers


codeigniter extending multiple controllers  using -'php,codeigniter,inheritance,out-of-memory,multiple-inheritance'

Can't find a way to do this, possibly because there is another way to do this?

Some of my controllers extend AdminLayout and some of them extend ModLayout but I also need these pages to extend a LoggedIn Controller.

class Profile extends AdminLayout, LoggedIn {


However looking into there is no way to do this nicely. Is there a workaround?
    

asked Sep 15, 2015 by WinifredKinn
0 votes
1 view



Related Hot Questions

2 Answers

0 votes

Assuming that you are using Codeigniter 2, this can be done by putting all you extended controller classes in the same file.

In /application/core create a file called MY_Controller.php (don't forget to check the subclass prefix in config.php around line 109)

In here you can add all you controller classes to extend. For example;

form_validation->set_error_delimiters('
', '
'); } } class LoggedIn extends MY_Controller { public function __construct() { parent::__construct(); if (is_logged_in() == FALSE) { $this->session->set_userdata('return_to', uri_string()); $this->session->set_flashdata('message', 'You need to log in.'); redirect('/home'); } } } class AdminLayout extends LoggedIn { public function __construct() { parent::__construct(); // do something } } class ModLayout extends LoggedIn { public function __construct() { parent::__construct(); // do something } } /* End of file */ /* Location: ./application/core/ */

Then when you create your controllers as per normal, just choose the base controller class to extend. Example;

class Foo extends AdminLayout {

    public function __construct() {
        parent::__construct();
        if (is_logged_in() == FALSE) {
            $this->session->set_userdata('return_to', uri_string());
            $this->session->set_flashdata('message', 'You need to log in.');
            redirect('/home');
        }
    }
}

or

class Bar extends ModLayout {

    public function __construct() {
        parent::__construct();
        if (is_logged_in() == FALSE) {
            $this->session->set_userdata('return_to', uri_string());
            $this->session->set_flashdata('message', 'You need to log in.');
            redirect('/home');
        }
    }
}
answered Sep 15, 2015 by CarSpriggs
0 votes

PHP does not support multiple inheritance. You could use Codeigniter helpers or libraries for this.

Have a look at the library examples:

http://codeigniter.com/wiki/Simplelogin

answered Sep 15, 2015 by AshleeMacart

...