CodeIgniter 2: How to extend CI_Controller multiple times?

前端 未结 5 1077
野性不改
野性不改 2020-12-08 10:38

I have successfully extended the CI_Controller class by creating a MY_Controller.php which I have placed in the application/core directory.

core/My_Controlle

5条回答
  •  感动是毒
    2020-12-08 11:17

    This is pretty easy. Do the following:

    1. Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
    2. Open this the file you just created and add your multiple classes, like so:

      class Admin_Parent extends CI_Controller {
          public function __construct() {
              parent::__construct();
          }
      
          public function test() {
              var_dump("from Admin_Parent");
          }
      }
      
      class User_Parent extends CI_Controller {
      
          public function __construct() {
              parent::__construct();
          }
      
          public function test(){
              var_dump("from User_Parent");
          }
      
      }
      
    3. Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php

    4. Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:

      class Adminchild extends Admin_Parent {
      
          function __construct() {
              parent::__construct();
          }
      
          function test() {
              parent::test();
          }
      
      }
      

提交回复
热议问题