代码之家  ›  专栏  ›  技术社区  ›  Tamik Soziev

如何在Codeigniter中创建两个父控制器?

  •  1
  • Tamik Soziev  · 技术社区  · 15 年前

    2 回复  |  直到 15 年前
        1
  •  5
  •   Phil Sturgeon    15 年前

    我写了一篇文章来说明你是怎么做到的。

    http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

    您需要在config.php中创建一个uu autoload()函数,或者直接在类定义之上包含基本控制器。

        2
  •  0
  •   CodeGodie    10 年前

    1. 转到以下目录: your_ci_app/application/core/ MY_Controller.php (此文件将是顶级父类所在的位置)
    2. 正常开放 并添加多个类,如下所示:

      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. 在此目录下创建子控制器 your_ci_app/application/controllers/ . 我称之为 adminchild.php

    4. 正常开放 adminchild.php文件 并创建控制器代码,确保扩展父类的名称,如下所示:

      class Adminchild extends Admin_Parent {
      
          function __construct() {
              parent::__construct();
          }
      
          function test() {
              parent::test();
          }
      
      }
      
    推荐文章