CodeIgniter Forums
Add variable to native Controller class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Add variable to native Controller class (/showthread.php?tid=2397)



Add variable to native Controller class - El Forum - 08-02-2007

[eluser]rokkan[/eluser]
I would like to add variables to the base Controller class so every controller can access it similar to $this->load and $this->db. I tried to overwrite the native Controller class but that doesn't seem possible (though it appears possible in the documentation). Has anyone done this before?


Add variable to native Controller class - El Forum - 08-02-2007

[eluser]coolfactor[/eluser]
Yes, I do it for all my sites.

Create a file called MY_Controller.php with a class MY_Controller that extends from CI_Controller, and put it into your /application/libraries/ folder. That should be all your need to do.

Just remember to extend your controller classes from MY_Controller if you want the added functionality.


Add variable to native Controller class - El Forum - 08-02-2007

[eluser]rokkan[/eluser]
I tried that but it doesn't work for controller (I made a custom router class so I have a little experience with overriding custom classes). Maybe I am mistaken but my native Controller doesn't have that CI_ prefix and I suspect that's why it doesn't work.


Add variable to native Controller class - El Forum - 08-02-2007

[eluser]coolfactor[/eluser]
Oh yah, you may need to extend from Controller instead of CI_Controller.


Add variable to native Controller class - El Forum - 08-02-2007

[eluser]rokkan[/eluser]
Tried that and still doesn't work as advertised. I filed a bug report on it and now I am hacking the native now.


Add variable to native Controller class - El Forum - 08-02-2007

[eluser]coolfactor[/eluser]
[quote author="rokkan" date="1186092410"]Tried that and still doesn't work as advertised. I filed a bug report on it and now I am hacking the native now.[/quote]

It's not a bug, as I do use that technique all the time.


Add variable to native Controller class - El Forum - 08-02-2007

[eluser]rokkan[/eluser]
Well is it that CI was recently updated and the Controller class is no longer prefixed with CI? Is your native Controller class prefixed with CI?


Add variable to native Controller class - El Forum - 08-02-2007

[eluser]coolfactor[/eluser]
I was wrong when I stated that you should extend from CI_Controller, it needs to be Controller.


Add variable to native Controller class - El Forum - 08-02-2007

[eluser]Rick Jolly[/eluser]
You could also extend the Controller the the usual php way:
Code:
class BaseController extends Controller
{
   function BaseController()
   {
      parent::Controller();
   }
}
Assuming the base controller is a file called base_controller.php in your controllers directory:
Code:
include(APPPATH . '/controllers/base_controller.php');

class SomeController extends BaseController
{
   function SomeController()
   {
      parent::BaseController();
   }
}



Add variable to native Controller class - El Forum - 08-02-2007

[eluser]coolfactor[/eluser]
Yes, I forgot about suggesting that way since extending the Controller "the CI way" has worked well for me. Not sure why it's not working for rokkan.