views:

782

answers:

3

I'm having a bit of confusion in attempting to retroactively create a new base controller for my project. If I'm not mistaken, all I need to do is create a file in application/libraries called MY_baseController.php containing the following:

class baseController extends Template_Controller
{
  public function __construct()
  {
    parent::__construct();
  }
}

And then rewrite my other controllers to extend baseController instead of Template_Controller:

class Frontpage_Controller extends Template_Controller

to

class Frontpage_Controller extends baseController

Yet when I do this, accessing the Frontpage_Controller alerts me that:

Class 'baseController' not found...

What am I missing here?

+2  A: 

After some fiddling, I think the following is my solution...

Move MY_baseController.php from application/libraries and into application/controllers. Rename it to base.php and change the following line:

class baseController extends Template_Controller

into

class Base_Controller extends Template_Controller

Now within your Frontpage Controller, extend Base_Controller instead of baseController.

Jonathan Sampson
Consider making it abstract as well, "abstract class Base_Controller extends Template_Controller". This prevents an accidental attempt to render it via http://example.com/myapp/base.
Chadwick
A: 

Make sure you follow Kohana Conventions to make sure everything auto-loads properly! There are similar ones in relation to Models Helpers and Libraries.

Also if you want to keep your main application controller folder clean I would suggest making a Kohana module just for your application and put all your template and misc extension controllers there to keep them separate from your main controllers.

Just don't forget to add the module to your config file!

Patrick
A: 

I know this is an old question, but I thought I'd put in a word. You just need to remove the MY_ prefix from the file name as you only really need it when extending a class suffixed with _Core in the system folder. For example, the file for

class Controller extends Controller_Core

would be named MY_Controller.php.

In this case, just naming the file baseController.php and putting it in the libraries folder would work.

Zahymaka