This article describes the Codeigniter controller controller inheritance problem with an example. Share it with everyone for your reference, the details are as follows:
In projects, there is often a situation where each page in the background must judge the Session to determine whether the user is logged in. For Codeigniter, each controller will be considered to inherit a common controller.
For example: AdminBase is the public controller of the application background. Each application background controller inherits the public AdminBase, but at the same time, make sure that AdminBase also inherits CI_Controller.
The same is true for HomeBase at the front desk.
The specific implementation is very simple, just create a new MY_Controller.php under application/core, as follows
(MY_ is configurable, go to application/config/config.php file and find this item: $config['subclass_prefix'] = 'MY_';)
class MY_Controller extends CI_Controller { function __construct() { parent::__construct(); } } class AdminBase extends MY_Controller { function __construct() { parent::__construct(); ...... } ...... } class HomeBase extends MY_Controller { function __construct() { parent::__construct(); ...... } ...... }
Then the controller in application/controllers can be inherited, such as application/controllers/admin/blog.php
class Blog extends AdminBase { function __construct() { parent::__construct(); ...... } ...... }
Readers who are interested in more content related to the CodeIgniter framework can check out the special topic on this site: "Introduction to codeigniter tutorial"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.