CodeIgniter Forums

Full Version: create a debuggin hook?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]steveeakin[/eluser]
I'm trying to create a hook or controller extension that acts as a debugging flag.

Ideally, it would be by appending a url like ?__debug_clear_cache, but we can live with extra uri segments like /__debug_/clear_cache/, or more likely, /debug/clear_cache/

The problem we're facing is how to get this to run. As a pre_controller hook, we have no access to the uri class, or even the load class. As a controller extension, we have created a library that extends the controller ('Extension_Controller') and has a function debug() in it and have it successfully autoloading. When in the real controller, we'll call it 'catalog', we get a non-existing class error when we extend 'Extension_Controller'. If we just extend the normal Controller class in our 'catalog' controller, both the 'catalog' and 'Extension_Controller' have their constructors running in the correct order even though we can't access index.php/catalog/debug/

Any hints on getting this done without hacking around the core libraries?

El Forum

[eluser]n0xie[/eluser]
Isn't it easier to just use a debugger (x-debug for instance) so you can actually debug, create breakpoints, step through code, see what variables and objects exist etc... ?

El Forum

[eluser]steveeakin[/eluser]
[quote author="n0xie" date="1245357432"]Isn't it easier to just use a debugger (x-debug for instance) so you can actually debug, create breakpoints, step through code, see what variables and objects exist etc... ?[/quote]

Well it would be if we were doing 'code' debugging. This class is really meant to do some beta environment debugging like clearing caches, turning on and off benchmarks, showing queries that are run on the page, etc.

El Forum

[eluser]n0xie[/eluser]
Aah I understand now...

Maybe do it the other way around. Add several debug functions to MY_Controller and let all your controllers extend from that?

El Forum

[eluser]Unknown[/eluser]
I'm having this issue. Is there a best practice in CI for handling needs like this?

El Forum

[eluser]steveeakin[/eluser]
[quote author="n0xie" date="1245357846"]Aah I understand now...

Maybe do it the other way around. Add several debug functions to MY_Controller and let all your controllers extend from that?[/quote]

I was trying that one with the autoloaded class:

Code:
class Extended_Controller extends Controller {
    public function __construct()
    {
        parent::Controller();
    }
    
    public function debug()
    {
       die (":] HAPPY TIMES!");
    }
}

class Catalog extends Extended_Controller {
    public function __construct()
    {
        parent::Extended_Controller();
    }
}

visiting index.php/catalog/debug -> Fatal error: class Extended_Controller not found.

I did some testing and the Extended_Controller IS autoloaded and its constructor runs BEFORE the Catalog constructor.

El Forum

[eluser]n0xie[/eluser]
Ok I just tried this and this works:

Code:
// system/application/library/MY_Controller.php
class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
    }

    function debug()
    {
        var_dump($this);
    }
}


// system/application/controllers/dashboard.php
class Dashboard extends MY_Controller {

    function Dashboard()
    {
        parent::MY_Controller();
    }
}

If I call http://mysite.tld/dashboard/debug I get all the data from var_dump($this).

El Forum

[eluser]steveeakin[/eluser]
WOW! 3 things:

1: Don't autoload it.

2: Don't use php5's __construct in the MY_Controller! Use good ole public MY_Controller()

3: Don't put the library in a subfolder. It didn't like that too much.

Thank you n0xie for the quick and thorough replies, this is going to be incredibly helpful and powerful.

El Forum

[eluser]n0xie[/eluser]
No problem. Obviously you wouldn't want to expose these function to the whole world so you might want to add some security here, but all in all it could be very useful to have some basic debug functions available via MY_Controller.

El Forum

[eluser]steveeakin[/eluser]
[quote author="n0xie" date="1245360314"]No problem. Obviously you wouldn't want to expose these function to the whole world so you might want to add some security here, but all in all it could be very useful to have some basic debug functions available via MY_Controller.[/quote]

Already have a 'enabled' flag set in a config and have extra url parameters for verification.

Good point to mention security for anyone who may be reading on later.