Welcome Guest, Not a member yet? Register   Sign In
Add variable to native Controller class
#1

[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?
#2

[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.
#3

[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.
#4

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

[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.
#6

[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.
#7

[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?
#8

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

[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();
   }
}
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB