Thursday 20 January 2011

CodeIgniter PHP Framework.

As part of a job hunt, I am having to take a look at CodeIgniter, a PHP development framework. The idea of a framework is that it saves you time by already implementing a lot of the basic code that would write for every application, yet doesn't constrain you as much as using a CMS like Drupal would.

I have looked at several frameworks in the past and developed projects using a couple of them Zend and Rails. Zend is quite good in that you can treat it as either a library or a full blown MVC framework. Rails is also good for prototyping sites and web developments -but if you want to get a fairly vanilla website up and running quickly then a CMS like Drupal would probably be first choice (perhaps followed up with a bespoke/framework version of the site for version 2)

Anyway, CodeIgniter -seems to be popular and bills itself as 'light weight' -not something you could accuse Zend of. It's an MVC system, that says you don't need a templating language or, necessarily a database!

Installation
 I'm trying this on Fedora Linux running under VMWare on my laptop. Interestingly the minimum PHP requirement is 4.3.2, although it runs under PHP 5 -which has been out for several years now.

Initial installation was pretty simple,  unzip the file in your web root and edit the config as per the instructions (n. b. the config file path should be system/application/config/config.php from the installation folder). That's it -you get a welcome page. Obviously there is no database integration at this stage.

What do you get?
 Initially a View and a Controller -but no model; fair enough we haven't an app yet. Take a look at the feature list, there's nothing there that's particularly outstanding looks like an MVC system with some bog standard libraries, most of which seem to be available in native PHP or PEAR/PECL; perhaps that's why there seem to be quite a lot of articles on using bits of Zend Framework within CodeIgniter.

Out of the box the URLs all contain index.php, e.g. http://localhost/CodeIgniter/index.php/blog/. I don't like this, but it is easy enough to remove using an Apache rewrite rule.

Controllers

These seem to work much as one would expect, and support a folder structure which should make for good code structure. For instance you should be able to have http://site/blog/post and site/blog/list in different files in a directory called blog, rather than lumping them into one big blog controller. Parameters are passed raw rather than as nvps (as far as I can see). There are pros and cons to this, the pro being shorter URLs the con being that it's less obvious from the URL what the function arguments are. Given the dynamic (ok chaotic) nature of most websites I can see bugs arising. Oh yeah -they live in the controller folder.

Views

Live in the view directory! Whilst there is a templating language, this seems to be regarded as unnecessary and, from the manual, PHP is used directly. Fine by me, PHP is a templating language. It seems that views have to be called explicitly. Boring, I like the default that says if I have a 'post' controller I'll call a 'post' view by default, if it exists; but I realise that this is just syntactic sugar. Beyond that it's just standard PHP, front end programming, data can be passed from the controller to the view as an array (of however many dimensions) just like Smarty so that's all good. You can also call multiple views from one controller function, thus mimicing Zend layouts, but this is starting to push display logic into the controller, breaking the MVC pattern.

Models
These seem to be a bit of an after thought and are described as optional. As an ageing database hacker the model is always where I start my development and design from, as it embodies the essence of an application and forces you describe the things that you are dealing with. So one black mark there. Models implement a cut-down Active Record pattern, table aren't implemented as classes (as per Rails and Zend). I think this is a shame as you lose some of the name-based goodness that you get with the one table/class model (meta model?). On the other hand there is a quite steep learning curve when it comes to more complex queries and the Rails/Zend version.

Models are abstracted from the underlying database engine, as it's PHP4 compatible I'm guessing not through PDO. You may need to still understand the underlying database though, i.e. transactions aren't going to work on a MySQL MYISAM table.

A couple of nice features are method chaining with PHP5 to produce readable code :
$this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);
Cache control to allow you to cache part of a query string, as is result caching.
Transactions, meta data and adapter function calls are all supported.


And there's more!
There are the usual helper, plugin and library mechanisms. There are limited hooks to allow you to call your own code within the control of flow through CodeIgniter. There's an AutoLoad mechanism, although the description sounds more like a pre-load, so not on-demand like Zend. It would seem that scaffolding has been dropped. You can override the routing and perform output caching.


What do I think?
That I'm knackered now. I think I might like CodeIgniter, it seems less heavyweigh than the tZend Framework and easier to get to grips with yet still retains most of the essentials. If you are desperate for a bit of Zend (maybe Lucene Search) my guess is that you could still use it from within CodeIgniter (although I think that I'd run Solr and write a class to talk to that -if there isn't one already).

So CodeIgniter, my first framework, and maybe you won't ever grow out of it.

No comments:

Post a Comment

linkedin