Welcome Guest, Not a member yet? Register   Sign In
Extending core Controller class
#1

[eluser]gabe[/eluser]
Hi there

I just wanted to extended that core class. My class prefix is MY_ and I created a file called MY_Controller.php in /system/application/libraries/, it contains the following: (I'm trying to enable profiling for all controllers without altering the controllers):
Code:
<?php
class MY_Controller extends Controller
{
    function __construct()
    {    
        $this->output->enable_profiler(TRUE);
    }
}
?>

It doesn't work Sad I probably have it all wrong. I'd be glad of some advice.

Regards
#2

[eluser]Rick Jolly[/eluser]
Code:
<?php
class MY_Controller extends Controller
{
    function __construct()
    {  
        parent::Controller();  
        $this->output->enable_profiler(TRUE);
    }
}
?>
#3

[eluser]gabe[/eluser]
Thanks for your prompt reply. I added the
Code:
parent::Controller();
but it still won't work. I even placed an exit(); statement in my version and the app didn't exit, somehow it isn't being read?
#4

[eluser]ontguy[/eluser]
I'd think the method you outlined should work not sure why it doesn't.

Here is one way to get it to work:

application\libraries\MY_Controller.php
Code:
class Public_Controller extends Controller {
    function Public_Controller() {
        parent::Controller();
        $this->output->enable_profiler(TRUE);
    }

controller\test.php
Code:
class Test extends Public_Controller {

  function __construct() {
        parent::Public_Controller();
    }
    
    function index ($offset = 0) {
     echo $offset;
    }
}
#5

[eluser]nevsie[/eluser]
this might be somehting i came up against... am i right in saying that you are on php4? php5 support the __construct() whereas for php4 you needed to mack the function the same name as the class, in your case:

Code:
<?php
class MY_Controller extends Controller
{
    function MY_Controller()
    {    
        parent::Controller();
        $this->output->enable_profiler(TRUE);
    }
}
?>

however, i found that i could not get the profiler to enable by default no matter what i did. only on a per controller basis. Someone might be able to suggest somehting here?
#6

[eluser]ontguy[/eluser]
What I suggested will work, every controller that extends Public_Controller would have the profiler enabled. It would work using __construct() too.

application\libraries\MY_Controller.php
Code:
class Public_Controller extends Controller {
    function __construct() {
        parent::Controller();
        $this->output->enable_profiler(TRUE);
    }

controller\test.php
Code:
class Test extends Public_Controller {

  function __construct() {
        parent::__construct();
    }
    
    function index ($offset = 0) {
     echo $offset;
    }
}
#7

[eluser]nevsie[/eluser]
will this work on multiple levels of controllers.

### strike through... a typo was causing my levels not to work... although i am still experiencing the __construct issues..
#8

[eluser]Michael Wales[/eluser]
I have found you can't use __construct() when extending CI, regardless whether you are running PHP4 or PHP5. At least for me, on xampp.

Since I develop on this machine and upload to live sites (without trying to change much), I've yet to try it on a live site.
#9

[eluser]gabe[/eluser]
Thanks to all for the replies.

I am using the latest PHP5 on an Ubuntu Gutsy server. So the __construct problem seemed unlikely. Also, with regards to the post by 'ontguy' I think your solution sounds reasonable, but not for me.

I wanted to extend the the CI_Controller to enable profiling globally on the dev environment, this way when I copy to the live site I can simply exclude my controller and profiling is disabled. If I make changes to each controller then they would end up on the live site and I have to keep manually disabling profiling.

Thanks to Michael Wales, it seemed possible, but even changing from __construct() didn't help, I've now got the following in system/application/libraries/MY_Controller.php:
Code:
<?php
class MY_Controller extends Controller
{
    function MY_Controller()
    {    
        parent::Controller();
        $this->output->enable_profiler(TRUE);
    }
}
?>
#10

[eluser]nevsie[/eluser]
and how are you referencing MY_Controller from all your other controllers?




Theme © iAndrew 2016 - Forum software by © MyBB