views:

1317

answers:

2

Hello,

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

+2  A: 

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.

mrinject
That does *work* - Thank you. But it's not what we are looking for. If we were in a single-app environment - your solution would be perfect.However we want all of our apps to share the same primary base controller. Short of replacing system/Controller.php - can this be done?
ChronoFish
LOL - This is exactly the solution I came up with as well. It seems to be the lowest impact - no code change on the Controller, limited change to the file, and it's available across all apps.Thanks for your help!
ChronoFish
A: 

Hi TIA.

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

Meglio