Sunday, 20 January 2013

CodeIgniter refactoring – one day fast-tracking!

I've recently been looking into PHP frameworks again and decided I needed to create an entire website built in CodeIgniter as it was the only real way to see it in action (the tutorial is good but obviously limited). Fortunately, I had a website that fit the bill in terms of size and requirements and although it was fairly far in its development cycle, I decided to take the plunge and refactor it into the CodeIgniter framework.

The end result was a complete refactored site in just one day, plus a much better understanding of how CodeIgniter works in a real world scenario!

I should also add that the site was originally written using large parts of the Zend Framework (v1.x), so this is in many ways a move from a complicated system to a more simple one than refactoring from say… a static site structure. I was also trying to fit in with the official working methods instead of going too much 'off-piste' as I believed this would say more about the framework support available without any crazy custom methods/libraries/hacks etc.

So, here's a few points i'd make of my one day CodeIgniter real world test.
  • Initially, there's very little that needed changing in order to get a skeleton project working. Unlike Zend Framework which requires a significant level of configuration and additional messing around to set up the framework so it does what you expect.
  • I love the simple use of environment variables. This is a huge advantage for developers that enables one codebase (albeit a changing constant) for any environment. This is part of Zend, but in CodeIgniter you can write php directly in the config files (e.g. useful for adding apache set variables) instead of using a slightly more restrictive config template.
  • I wanted to use a base template for use on all pages and needed to create a custom controller to extend the default controller. This, I would say is something that should be supported better in the core instead of requiring a custom class straight off the bat, and for a fairly simple site.
  • I look forward to using the template features that CodeIgniter has. This is something that requires significantly more configuration in a framework such as Zend. I'm also keen to see how easy it is to create a multi-language site.
  • There's definitely a case for a class to create navigation elements. One of the things I really like about Zend is the Zend_Navigation class which can be utilised for things like site maps, breadcrumbs, security etc. I'll probably have to create my own or use a custom library for this if the project requires it.
  • I still haven't completely got to grips with how CodeIgniter references custom controllers, helpers and libraries, but i'm sure that will reveal itself in time. Zend has custom namespaces which enables the engine to 'see' custom code, yet this doesn't work in all cases as it mainly seems beneficial to libraries and not controllers or helpers buried somewhere other than where the namespace code sits.
  • I wasn't particularly impressed with the way error pages were handled. Errors should really have basic views and for these to exist in their own error directory with only (inferred) the 404 page able to be changed in the routes config seems a bit weird to me.
  • I'm not sure if CodeIgniter uses a view class, but if not then I think it might be helpful. It certainly seems to create page content from views in a sequential (no going back) approach, and unlike Zend, local scope variables are accessible by standard variable syntax '$bob' rather than '$this->bob'.
  • Perhaps the largest omission is the lack of modular support from CodeIgniter. I'm aware that there are third party solutions for this, but it would be nice to see a next revision supporting that ability. There's nothing better than being able to wall-off things that logically belong together with their own routes, controllers, views etc.
I haven't tried many of the additional classes or helpers such as form validation & image manipulation which lend themselves more to administration systems, but I hope to soon.

To conclude, I would strongly recommend the above approach to learning CodeIgniter if you have a good test case website and a couple of spare days. Worst case, you get stuck but still have the original site (which you should have backed up) that you can go back to so nothing is lost.

No comments:

Post a Comment