You need to create basic controllers that extend the CI_Controller. Then all your controllers expand the specific base controller that you created, depending on what you need to do in all cases when the controller is called.
In application/core create a file called MY_controller.php (the prefix can be changed in config):
class MY_Controller extends CI_Controller { function __construct() { parent::__construct(); $this->layout->prepare_widget( "widgets/navigation", "navigation_widget" ); $this->layout->prepare_widget( "widgets/footer", "footer_widget" ); $this->layout->prepare_widget( "widgets/twitter", "twitter_widget" ); } } class Public_Controller extends MY_Controller { function __construct() { parent::__construct(); } } class Admin_Controller extends MY_Controller { function __construct() { parent::__construct(); if( !$this->user->has_permissions( PERMISSION_ADMIN ) ) { redirect( base_url(), "location", 303 ); die(); } } } class Member_Controller extends MY_Controller { function __construct() { parent::__construct(); if( !$this->user->has_permissions( PERMISSION_REGISTERED ) ) { redirect( base_url(), "location", 303 ); die(); } } }
As you can see, all subcontrollers have widgets automatically because they extend either public, admin or member. An additional controller that extends the administrator controller automatically checks permissions, so you no longer need to do this. You can apply this concept to your application.
Sub-controller: (fits into regular application/controllers )
class Articles extends Member_controller { ... }
It will automatically ensure that the user is logged in and the widgets are prepared without any action, because the parent parent class has already prepared them. All you need to do in the articles is to call $this->layout->render if the logic needs to render the layout at the end.
source share