MVC WTF?

mvc wtf?
As a freelance developer, inheriting and working with code I did not originally write is common place. More often than not, code written using a known framework results in perfectly readable code, easy enough to work with and extend. Granted, every project is not written in Laravel 5, but the code that I inherit is workable.

When I recently inherited a complicated CodeIgniter site I was (initially) confident I’d have a good base to extend and work with. How wrong I was I? I could literally write a book about why the project I inherited was so bad. For the sake of brevity, I’ll stick to the main issues.

This post proves a single statement. Code written using a framework, is not necessarily “good”. It can end up painfully awful. In retrospect, I would have preferred to inherit a WordPress site. At least then my expectations are low to begin with 🙂

Continue reading MVC WTF?

Validate Data within A Model using CodeIgniter and MY_Model

CodeIgniter’s validation library is amazing out of the box and will save any developer an absolute ton of time. However, as CodeIgniter is an MVC framework it’s validation library does encourage data validation directly in controllers – which of course for the “MVC Nazis” out there is against strict MVC principals. So, data validation should strictly happen at the model layer, not in the controller. Moving data validation to the model has a few benefits  – it allows your application to follow to coveted “Fat Model, Skinny Controller” pattern and allows you to validate other data types (not just posted data). Again, you can technically keep things neat by saving your validation rules to a config file and keeping validation in the controller, but that still breaks the MVC principals.

Continue reading Validate Data within A Model using CodeIgniter and MY_Model

Why Codeigniter Base Models Rock

codeigniter base crud modelsThere are a plethora of reasons to use a good base model (also called CRUD Model, MY-Model) for all your CRUD operations in a CodeIgniter (or any) application. Amongst many, a base model will boostrap all models it extends, keep your application as “DRY” as possible and speed up general development. Codeigniter has a pretty neat active record implementation, but you do tend to repeat a lot of the boring database stuff when writing models. In my opinion, you’d be insane not to use a good base model. There are many out there, but I use the amazing base model from Jamie Rumblelow (it deserves and SEO Link before you ask!).

Continue reading Why Codeigniter Base Models Rock

How to Install CodeIgniter Within the Web Root

By default, when installing CodeIgniter (CI), all the important files i.e. all files withing the application and systems folders are installed within the main web directory, that is visible to anyone. This is fine to an extent because all folders have a htaccess file that denys all incoming requests. The CodeIgniter developers did this intentionally to make things easy for people trying the framework out.

However, a much more secure method is to store the application and systems folders within the server webroot. This location is not directly accessible and helps with all around application security. Personally, I feel a lot safer knowing core files are not directly accessible. The latter is doubly as important when using a well known PHP framework, as anyone has a starting point to figure out your folder structure, simply by downloading the framework.

Here is how a typical CodeIgniter install looks on a production server:

Continue reading How to Install CodeIgniter Within the Web Root