Base controllers and autoloading libraries

Difficulty: Easy

In the upcoming release of Kohana, Kohana 2.2 the Loader library will no longer be present. Effectively this means you can no longer use the $this->load->library() syntax. Do not get scared now, the only thing the library did was ‘new {class}’ and place it in the $this->{class} property.

The other thing that’s disappearing together with the loader is the autoloading of libraries or models using the preload setting in your config.php file. Again, keep calm, I will show you how you can still use this behaviour and even make it better.

To do this we need a base controller. A base controller is basically the controller all or some of your real controller subclass. For example, for an application of mine I wrote two base controllers, one for the frontend and one for the backend. These base controllers did a few things such as loading the Session library and my Layout library It also determines whether the request is an Ajax request and turns of the layout, or when the request is for a feed it will change the view to the rss view. All of these are examples of what you can do with base controllers. There are some examples in the forums.

So, let’s create a base controller
application/controllers/base.php

abstract class Base_Controller extends Controller {
 
	public function __construct()
	{
		parent::__construct(); //it's necessary to call the construct of the Controller class
 
		$this->session=Session::instance(); //Loading the Session library in the base controller
 
                if(request::is_ajax()) //available for Kohana 2.2 and up
                {
                     //do something when request is ajax
                }
	}
}

That’s all you need for a simple base controller. It’s abstract so it cannot be instantiated, only classes that subclass this controller can be instantiated. Now, what’s the use of this :) Let’s create an Article controller

class Article_Controller extends Base_Controller{
//I don't need the constructor so I can omit it
public function index(){
    $this->session->get('session_var'); //I've loaded the session library in the base controller
}
 
}

I hope it’s clear how preloading can be replaced by a base controller as well as how it can be used for other things like ajax requests.

This is post #50 by the way. I’m quite proud to have come this far. Next milestone: a 100 posts :)


7 Responses to “Base controllers and autoloading libraries”

  1. Sam Clark Says:

    This is good news dlib, we are already using our own custom controller classes to do exactly this task and it’s nice to see everyone heading in the same direction.

    Congrats on your 50th post as well :-D

  2. spirit Says:

    You could also have it extend Template_Controller for templating the pages

  3. Ben Rogers Says:

    shouldnt it be:

    class Controller extends Controller_Core

    instead of:
    class Base_Controller extends Controller

    i thought they auto linked a class to classname_Core so they could be easily extended with out having to change everyone who references the object.

  4. dlib Says:

    Ben, you refer to the MY_Controller transparent extending. You’re right, this is possible. Problem with this approach in this case is that all your controllers use the extended Controller extends Controller_Core. If you need a piece of code in all your controllers this approach is best.

    The approach in the article allows you to have a Frontend_Controller, Backend_Controller etc. and different pages extend different controllers.

  5. RastaTech Says:

    so, dlib, just to clarify (I’m a slow learner with some things), the base controller concept *eliminates* the necessity for overloading the Controller_Core via ‘MY_controller’? Or is there a scenario when you’d want to use both, that is, overload with MY_Controller AND use a Base_Controller?

  6. dlib Says:

    The MY_Controller approach still is possible and has the advantage (or disadvantage) that it works for all controllers in your application. Say you use the session class on all your pages, the MY_Controller would be the place to put it. In this case you can use both.

    What I find an advantage of Base_Controller is that you place it in the controllers directory and not in the libraries directory. This keeps controller (related) code centralized.

  7. RastaTech Says:

    Cool that makes much more sense!
    Thanks again for the effort you are putting into this blog, and all your very enlightening comments on the Kohana forum — they are appreciated!
    I also applaud your attitude; everybody’s a n00b @ some point (some for longer than others!)
    While I can certainly understand Shadowhand’s impatience with people new to OOP/PHP5 (I worked in desktop support for YEARS, so i know what it’s like to deal with nincompoops all day), that impatience tends to make his answers more terse and therefore less enlightening than they might otherwise be. This adds to people’s frustrations and makes them ask MORE silly/stupid questions except now in an angry way, which amplifies his impatience and resentment, etc.
    Your attitude of helpful friendliness short-circuits that vicious circle, and makes it much easier for people like me to learn. Thanks again and pats on the back to you for all the great work making a really elegant piece of work like Kohana accessible to those of us who know enough to appreciate its design, but are still struggling with its implementation…

Leave a Comment