English 中文(简体)
如何在Codeigniter中创建两个父控制器?
原标题:How to create two parent controllers in Codeigniter?

我想创建两个父控制器:一个用于管理员,一个用于用户站点。它们必须扩展一个常规的Controller类,但每个类都必须做不同的事情。

最佳回答

我写了一篇文章展示你是如何做到这一点的。

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

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

问题回答

这很容易。请执行以下操作:

  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. 打开MY_Controller.php并添加多个类,如下所示:

    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();
        }
    
    }
    




相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签