Separating the core application from the delivery framework with Kohana

This post is about developing web applications that don’t depend on the web.

Kohana MVC - A separated delivery mechanism architecture

MVC is probably the most popular architecture for web applications. But what’s interesting about MVC is that it’s not actually an architecture meant for your core application. It is merely a delivery mechanism.

With this in mind – a well developed application treats the delivery mechanism as a plugin and cleanly separates the core application from the web. It should be possible to remove the internet with all of its methods of interaction (eg: its HTTP Request/Response interface), and still have a working “core” application which you can use elsewhere. Say for example to make a desktop or mobile device application.

In short: your business logic doesn’t rely on the internet to exist.

With Kohana, or really any modern MVC framework which supports the PSR-0 standard, this is surprisingly easy to do. I’d like to share the convention I use.

In Kohana, all logic goes in application/classes/ (or equivalent in its CFS). This directory will contain all your delivery logic. This includes Controllers, Views, and any Models, and perhaps some Factories to DI your app.

Your actual core logic is kept in a separate repository to force yourself to remove all dependencies. When combined, I like to store the core logic in application/vendor/. With Git this can be done with a submodule. This way MVC and insert-your-architecture-here is cleanly separated.

You can then add your core application to Kohana’s autoloader (in application/bootstrap.php for convenience via spl_autoload_register(function($class) { Kohana::auto_load($class, 'vendor/Path/To/App/src'); });

And that’s it! With a little discipline we suddenly get a massive benefit of future flexibility.

If you are interested in a project which uses this separation, please see my WIPUP project. (Disclaimer: WIPUP is a side-project and is still in progress). More reading by Bob Martin here.


CodeIgniter – Simple, clean, MVC framework for PHP.

People learning PHP go through the wonderful stages of <?php echo 'Hello, world!'; ?>, the then branch off into either: 1) Wow! It’s so dynamic! Let’s create everything right here right now! or 2) Wow! It’s so messed up I don’t know $int from $char! Unfortunately, the people who belong in (1) tend to spend the next couple years in an ignorant bliss, spewing out hundreds of lines of non-OOP code with a structure somewhat like this:

include './inc/template/header.php';
if ( $p == '' )
include './inc/pages/index.php';
elseif ( $p == 'about')
include './inc/pages/about.php';
[... etc ...]
include './inc/template/404.php';
include 'footer.php';

Well. Look familiar to anybody? (or even worse examples!) Hah! My good sir, you have just missed out on the programming epiphany of the century. Any script kiddy with the guts to program before learning OOP should have their guts removed, quartered, and stuck up as a warning to other coders. So, if you are doing PHP, but you don’t know what OOP or MVC is – here is your cue to fricass√© your arse and get ready for some late nights of programming ecstasy.

This post is somewhat a half-rant about the disadvantages of coding without any OOP or proper framework, and also a sort of happy unrelated babble talk brought about by me recently opening up (g)Vim again to code some very simple PHP, then taking a look at POSE2. For those that don’t know, POSE2 was the project to basically make E2 as open as possible. It’s an MVC (Model-View-Controller) framework, with no built-in helper classes. It’s extremely minimalistic. It was used to construct VisionBin, and currently runs on it too.

Hilariously enough, I fell into the common programmer trap of recreating the wheel. That’s why now, instead of advertising my oh-so-awesome POSE2 system, I’m going to be recommending you CodeIgniter. CI is a fast, clean, superbly documented MVC PHP framework. Or so I say before trying it out properly. Let’s actually see how awesome it is. I will type this post out live as I convert the extremely simple E2 site from POSE2 to CI.

Ok, apparently my connection hates me as it keeps on failing to download the tiny CI source files. After several retries, I finally am ready to start …. ok, uploading onto E2. This is great as I don’t get a lot of extra unneeded files in the / directory (except for index.php, obviously!), so everything is nicely tucked away in the system directory. Ok, quite a lot of files being uploaded here, but compared to other frameworks, this is slim. Let’s try visiting it without bothering to read any installation documents. Yep, already, it’s working without any needed config. I don’t see any installation docs (perhaps I’m just blur) but I do notice a config/ directory. It’s wonderfully documented and there’s quite a minimal amount to setup. .htaccess is a pain to setup due to specific server configurations (took quite a bit of debugging, but the option to fix it was hidden in the configuration), but other than that, it worked flawlessly. Easy to create new views, models, and controllers. Lots of helper classes – perfect, looks like exactly what anybody needs. Embeddable views to enable for templating…perfect.

First impression: wonderful, fast, user-friendly and amazingly well documented. Will give future updates on how I find it as I use it more. Meanwhile, I definitely recommend it to people.