Question

In my CI system\libraries directory I have a new class named DD_Controller.php. This file looks like this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class DD_Controller extends Controller 
{   
    protected $ddauthentication;


    function __construct()
    {           
        parent::Controller();
        $this->ddauthentication = "Authenticated";
    }
}
?>

My application controller is defined like this:

class Inquiry extends DD_Controller 
{...}

The Inquiry class works fine when I extend Controller, but I get a

Fatal error: Class 'DD_Controller' not found in C:\development\localhost\applications\inquiry\controllers\inquiry.php on line 4

When I extend DD_Controller. In the config file I have the prefix defined as such:

$config['subclass_prefix'] = 'DD_';

Any idea of what I'm missing?

TIA

Was it helpful?

Solution

DD_Controller.php should be in /system/application/libraries/

If you're using the same CI for multiple apps, and you want them all to be able to extends their controllers to your custom one then you can extend the base Controller class in the same file.

In system/libraries/Controller.php below the Controller class:

class Mega_Controller extends Controller {
    function Mega_Controller()
    {
        parent::Controller();
        // anything you want to do in every controller, ye shall perform here.
    }
}

Then you'll be able to do this in your app controllers:

class Home extends Mega_Controller {
    ....

Since the extended controller class you created will be available. I think this is better then overwriting the base controller, but that would work as well.

OTHER TIPS

This is a better approach. 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();
        }
    
    }
    

I recommend to avoid "cracking" CodeIgniter core files. Better use its native extending possibilities and try to fit into them.

The same rule I would recommend for any PHP library / CMS. This rule has few reasons: - ability to quiclky upgrade without takint into account thousands of notes where and how was cracked in core files; - portability; - possibility to share your code - eg, this will be usable by both you and your friends in case of need, and it will help them to keep their library up to date, the same as you.

In other words, this is much more professional and it pays to you in the future by usability, portability and by update application possibility.

Regarding your personal question...

As for me, there is nothing bad to create your own library with everything you need to extend native CodeIgniter Controller, then load this library in Controller's constructor and you are done. The only thing to make better usability is to give short name to your library.

This way you can even divide what you need in different pieces and put into separate libraries: WebFeatures AdminFeatures etc.

Then you just load needed libraries in your controller's constructor and you are done.

P.S. I know that proposed way does not fit into "right" OOP concept, but in the same time you must never forget about the integrity of the libraries used.

Everything above is just one more view of mine 7-years experience in professional web development, so I hope it will be helpful if not to follow, then at least to take into account.

Regards, Anton

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top