Goodbye CodeIgniter, Hello Laravel

A lot of PHP developers are fan boys of their PHP framework, and there are quite a few from which you can choose. Among the most popular ones are CodeIgniter, Zend Framework, CakePHP, Symfony, Yii, and the new kids on the block Silex, Slim, Lithium, and Laravel.

In the beginning of my career I stumble upon CodeIgniter and I love it for its simplicity, small footprint, and good documentation. It worked great for me when I developed a small to medium sized applications (usually Facebook apps), and the learning curve for new developers who worked with me was shallow enough that they could easily be integrated in the development process.

But last year, because of the Twitter buzz from some in the PHP community, blog posts, and the suggestions of some friends, I give Laravel 3 a try – and since that time I’ve never looked back. So, in this article I’d like to present a comparison of the two frameworks from my point of view.

Requirements

CodeIgniter 2.1.3 needs PHP 5.1.6, but versions before 2 still worked with PHP 4. This was a drag since everybody moved on to use PHP 5. Laravel 3 needs PHP 5.3 and also the Mcrypt extension.

CodeIgniter 1 didn’t need Mcrypt, since if you didn’t have the extension installed then it would use a custom functions to encode/decode strings. Of course this slows down the application, and we were amazed at some of the production servers that didn’t have it installed.

<?php
function encode($string, $key = '') {
    $key = $this->get_key($key);
    if ($this->_mcrypt_exists === true) {
        $enc = $this->mcrypt_encode($string, $key);
    }
    else {
        $enc = $this>_xor_encode($string, $key);
    }
    return base64_encode($enc);
}

REST

It’s easy to build a REST API with Laravel using RESTful Controllers where you simply have the $restful property set true inside your controller. For example, if you’re building an API for a task manager, for updating a task it would use something like this:

<?php
class Tasks_Controller extends Base_Controller {
    public $restful = true;
    public function get_index()
    {
    }
    public function put_update()
    {
    }
...
}

To handle PUT in CodeIgniter you would need this workaround:

<?php
if ($_SERVER["REQUEST_METHOD"] == "PUT") {
    parse_str(file_get_contents("php://input"), $post_vars);
    $arData = json_decode(array_pop(array_keys($post_vars)));
}

As you can see, this is error prone and not very clean.

Code Organization

Most of the time, the project you are working on will have a frontend and backend. In order to better organize the code in CodeIgniter, the best approach is to use HMVC. In this way you would have separate directories for backend code and frontend code so that different developers can work independently. To achieve something similar with Laravel, you can use nested controllers.

The View

CodeIgniter doesn’t have a default template engine, but you can easily integrate one like Smarty. On the other hand, Laravel uses Blade as its template engine which is very simple but powerful. With Blade, it’s very easy to set up a two-step view.

You have a master template where usually you put the basic HTML structure with a header, navigation, footer, etc. Most of the time this template is called layout.blade.php or master.blade.php. The portion of code that will be injected from different different controllers is declared in the master template by @yield('content').

<!DOCTYPE HTML>
<html>
 <head>
  ...
 </head>
 <body>
  <div class="header">
   ...
  </div>
  @yield('content')
 </body>
</html>

In custom templates declared per each method of a controller, for example a CMS page like “About Us”, you have a template called page.blade.php which looks something like this:

@layout('master')
...
@section('content')
About page section
@endsection
...

You can have as many section as you like to declare and reuse.

A more detailed explanation about this can be found in the section Blade Layouts in the online open source book called Code Happy by Dayle Rees.

To achieve this in CodeIgniter, at least as far as I’ve been able to figure out, is to create a master controller which all other controllers extend and call a render method. A CodeIgniter start project for implementing two step views can be found under my GitHub account.

The Model

Codeigniter uses Active Record for database manipulation which is quite easy to master, but you can also use raw queries if you have complicated requirements. Laravel uses Eloquent as a ORM which is simple and works with major database servers. For writing queries, you can also use Fluent Query Builder, which looks like a lot like the Active Record approach. And of course, you can also write raw queries too.

Command Line (Crons)

For Codeigniter to call a cron script, the best approach is to create a Cron controller. You also need to duplicate index.php and create a cron.php file right in the root directory, overriding the following the following $_SERVER values:

<?php
$_SERVER['SERVER_NAME'] = "xxxxxx.org";
$_SERVER['REQUEST_URI'] = "cron/index";
$_SERVER['QUERY_STRING'] = "lang=en";
$_SERVER['SERVER_PORT'] = "0";
$_SERVER['REQUEST_METHOD'] = "GET"

Then you can call it from the command line with php cron.php cron index.

In Laravel it’s easier. You have Tasks using the command line tool that Laravel comes with called Artisan.

Miscellaneous

  • Unit Testing – Codeigniter uses a very rudimentary testing class for writing unit tests. It is limited, but you can integrate PHPUnit. Laravel comes with PHPUnit out of the box, the most popular PHP unit testing framework.
  • Authentication – Codeigniter doesn’t have anything out of the box for authentication, but you can always use a third-party library, like Ion Auth. Larvel on the other hand has a basic auth library that you only need to configure before using.
  • Caching – With CodeIgniter, the driver supports APC, Memcache, and the filesystem. Laravel on the other hand supports those and also database tables and Redis.
  • Hooks/Filters – If you want to step in and run certain code at different points of execution in CodeIgniter, you should do so using Hooks. Laravel offers Filters.
  • Routing – Routing is done pretty much the same way in both Laravel and CodeIgniter, although I’ve found Laravel to be more flexible.
  • Configuration – Configuration is done via predefined arrays stored in config files which support multiple environments in both frameworks.
  • Extensibility – Extending the core functionality of CodeIgniter is done with Libraries, and you can find a lot of them on Sparks (a package management system for CodeIgniter). Laravel uses Bundles, which might seem familiar to you if you’ve worked with Symfony 2. You can find check already-created bundles at bundles.laravel.com.
  • MigrationsDatabase migrations are specific for Laravel, a concept borrow from Ruby on Rails which is versioning at the database level.

Community

CodeIgniter used to have a bigger community, but many moved to different frameworks after EllisLab, the company behind it, dropped support and no new features were added. Some of those people joined the Laravel community which is now very active with a lot of tutorials, books, and even its first international conference.

The future seems to be very bright for Laravel since Laravel 4 is in beta 2 and its first conference has just ended. On the other side for CodeIgniter, there are few member contributors who so commit/merge patches into CodeIgniter 2 but there is no release date for CodeIgniter 3.

Conclusion

With Laravel 4 on the horizon, the framework definitely has the momentum now and the community behind it is very active. I’m kind of nostalgic when I talk about CodeIgniter, but it served me very well at the time. It taught me how to use MVC, was easy to learn, earned me my first money, and I even did my first book review about it, but as time went on it hardly changed. Maybe it’s just because it was very good at what it did? But web development changes rapidly, and new technologies come around, so if you want to survive you have to keep up.

Just like in politics, everyone chooses a side. And for most of the time you can stick with your choice, but it’s always good to explore new things. For me, it meant goodbye CodeIgniter, hello Laravel.

Comments on this article are closed. Have a question about PHP frameworks? Why not ask it on our forums?

Image via Fotolia

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

  • http://joecianflone.co JoeCianflone

    I’ve been using the beta of Laravel 4 and it’s very awesome (I also left CI behind). It’s important to mention that Laravel 4 does not use bundle. It specifically uses Composer and Packagist. Also, Laravel itself is totally broken up into components and takes advantage of Composer. I’d check out the Laracon Screencasts on YouTube and check out the Laravel 4 demos that Taylor (the guy behind Laravel) has created on Vimeo. And before anyone askes…I’ve been using the L4 BETA for a project right now…I would say it’s very close to production ready. If you have a new think you’re starting, you’d probably want to use that.

    • http://alicia.wilkersons.us Alicia

      Tuts Plus Network is also doing a mostly free series on What’s different in Laravel 4 (compared to 3). It’s good to check out if you have been using Lavavel 3. Tuts Plus also has a series on Laravel Essentials. (https://tutsplus.com/course/whats-new-in-laravel-4/)

  • Levani

    “CodeIgniter used to have a bigger community, but many moved to different frameworks after EllisLab, the company behind it, dropped support and no new features were added.”
    Did EllisLab really drop support for codeigniter?

    • http://www.gafitescu.ro Daniel

      “Did EllisLab really drop support for Codeigniter?”

      Community drive now the development of the Codeigniter , after Derek Allard & Dan Horrigan has left EllisLab in 2010 the progress of the framework just wasn’t the same any more.

      Check more on this on Phil Sturgeon blog post : http://philsturgeon.co.uk/blog/2010/10/what-happens-next

    • http://philsturgeon.co.uk Phil Sturgeon

      Levani: EllisLab have not posted a single commit in the last 6 months, and refuse to style the documentation meaning 3.0 can not be released. It’s a ridiculous position, but its been functionally ready for months.

      Daniel: I wrote that 3 years ago dude. A more relevant article to link to would be this:

      http://philsturgeon.co.uk/blog/2012/12/5-things-codeigniter-cannot-do-without-a-rewrite

      • http://www.gafitescu.ro Daniel Gafitescu

        Hi Phil!
        I think I had both link open in separate browser tabs and used the first one which had “A Plea to EllisLab” and the fact that few engineers left EllisLab.

        Looking forward to see PyroCMS on Laravel4 , it was for sure your decision to move the CMS from Codeigniter to Laravel that made me look into it and pick it up for new projects.

  • feribg

    Another pointless framework that doesn’t change anything (same can be achieved with CodeIgniter, Cake, Yii, basically any decent PHP framework. If you want to build something serious with PHP go the Symfony2 way. You are guaranteed backward compatibility, security and bugs are fixed very promptly, mature framework with probably the biggest community, tons of bundles, solid architecture based mostly on what Spring MVC offers with a PHP twist.

    • http://www.gafitescu.ro Daniel

      Symfony2 is great but can be a overkill if you have a small/medium project. For this kind of projects I would usually choose a small footprint framework like Laravel , Silex or Slim.

      Laravel4 uses Symfony2 Components just like Drupal8 , phpBB or eZ Publish.

    • Manu

      Well, I come from the Java world. I was tired of over-complicated frameworks like Spring or Struts. When I had to switch to PHP one year ago I started with Symfony as it was THE reference. It is a technically clean framework but as you say, it is a copy of Spring. If I want to play with a Spring-like framework, I use Java. With PHP I want to work “fast&light”. So I leaved Symfony and gave a try to Laravel. It is not perfect but allows to work fast and is not resource angry. It is a pleasure to work with it. With Symfony I feel like in jail with everybody who say “you must do this like this because it is the rule of the perfect code”. With Laravel I feel free. With Laravel I can fly. :-)
      This remember me when I was using Hibernate: wow, nice code, wow, powerful, oups, something wrong, where the bug come from ? Wow, I have to enter into the code of Hibernate, shit, over-complicated ! Shit it eats too much memory ! Well, I have to change all my code, now my code is ugly. Same for Doctrine: too complicated when going out of simple SQL requests, and eat too much RAM.
      Fluent of Laravel is light, fast and good enough for my needs.
      (sorry for my bad english, I am a french-speaking people)

      • http://www.dzpage.com Mourad HAMOUD

        I used CI for a while because it didn’t get in the way, and feels very close to pure PHP. Now I consider CI a dead framework.

        I will take a closer look at Laravel, and see what it has to offer.

    • umren
    • Fernando Miño

      And sloooooow as slowpoke (personal experience), please developers for serious app use serious and fast platform, use java.

  • bicccio

    How about your old Codeigniter project???

  • http://victorjonsson.se Victor Jonsson

    “Goodbye frameworks, Hello packages”

    Why even bother about frameworks now a days? If all PHP-developer would adhere to the standards that evolves around composer and packagist.org you wouldn’t need to choose which framework to use and the big discussion around which framework that does the job best would soon be over. My latest project I simply chose one package that managed routing, another package for the database layer + some other packages that suited my project good. Instead of having an application built on top of a bloated framework with multitudes of dependencies and rules to adjust too I got an application consisting of small standalone components, each one with a small set of well defined dependencies. I say it again, “Goodbye frameworks, Hello packages”

    • ValidCharacter

      + one for the most forward thinking comment on this thread. I would never have considered this approach, though now, it seems the most logical.

    • Francesco Lettera

      Totally agree with Victor: “Goodbye frameworks, Hello packages”

    • http://LemonadeChronicles.com SpeckledFish

      Victor,
      Packages? Are you referring to Laravel packages or something else? You just use them as a plug-n-play – require_once “package_name” and then use the classes and methods?

    • http://www.dzpage.com Mourad HAMOUD

      Can anyone write a good article titled “Goodbye frameworks, Hello packages”?

      I think it will be helpful for a developer to have a guide when considering this approach.

  • Todor Pavlov

    Most of the PHP frameworks (including CodeIgniter and Laravel) are slow and those that are not (like Slim) are useless. There is only one exception – PhalconPHP which is full featured PHP framework which is twice faster than Slim (not to talk about the others): http://systemsarchitect.net/performance-benchmark-of-popular-php-frameworks/

  • http://www.artlantis.web.tr Oğuz Çelikdemir

    There is one thing that I want to know ( if we compare all frameworks ). Considering learning curve, which one is more better than others? I know CodeIgniter which is very easy to learn. For instance, I tried Zend Framework and Symfony but it was very complicated to learn something.

    • http://www.gafitescu.ro Daniel Gafitescu

      In my opinion Codeigniter is much easier to pick up then Laravel , and if you do learn Laravel go and learn Laravel4 since in few weeks it’s going to be out of beta2.
      But on the long run I think it worth to go and learn Laravel even if it takes longer.

    • http://hungrycoder.net Asif

      Try laravel. its so elegent

  • Jo

    Goodbye codeigniter, goodbye laravel and welcomme f3!

  • david

    CI was my first framework. As you learn to be better programer you will stop using it. I think using CI is a “must” but just for a few months. After you need serious OOP with the controls and models , well.. you will search for another framework.

  • idriss

    i use codeIgniter and i still love! so why change to another framework that is not even stable (must do a lot of thigns to change from 3 to 4).

  • Mladen

    Say hello to HMVC and Kohana framework.

  • http://thejw23.tumblr.com/ thejw23

    “Goodbye frameworks, Hello packages” -> if so, then don’t forget about http://auraphp.com/

  • kostas

    I started with ZF1 (it took me months to jump on the “boat”) , I do not regret though , each minute on my ZFs journey was the best investment I had ever made .
    I made also an intermediate pause on Symphony2 (it is just amazing) .
    Finally , I gave a try to Laravel4 (which I find super-super simple to understand) . Of course my “two years experience” with the two aforementioned frameworks gave me a an advantage .
    My final thoughts up to now: there is no ideal framework , each one has its own mentality . The best combination is to find a framework that “glues” the best to your own mentality . I haven’t found it yet , but one is for sure , every framework added its value into my knowledge .
    My two cent opinion .

  • http://dltr.org/ Damu

    Looks like i should switch to laravel too. Haven’t visited any frameworks for a while though. Now it’s time to check out and decide.

  • Jake

    So tired of all the laravel fanboys! CodeIgniter is a fantastic framework with great support. Laravel seems pretty nice but lacks community support. Use the framework that works for you.

  • http://www.daduu.com realtebo

    It could be very nice a direct compare from Yii and Laravel.
    What dou you thyink of Yii?

  • http://Dictionary.com Scott Cliburn

    So, only medium size projects for Codeigniter. BS. Dictionary.com is huge, gets millions of requests a month, and uses/used Codeigniter. Just because you have a lacking ability to implement on a large project, doesn’t mean it can’t be done. It was also run in multiple (3+) data centers.

    You can also run Doctrine ORM in it if you don’t like the vanilla flavor!

  • Pooky

    Laravel is ok once you finish init setup but if you beginner then you will find little bit confused. If you want Laravel like framework without complicated setup then try to use Slim, Slim is very good and fast microframework. I try Zend, CakePHP, Symfony, Yii, Kohana …. but always return to CI :)
    Before few weeks I start to learn Java and find Vaddin Java FW, it’s look amazing, I plan to switch to Java after this :D

  • http://www.hyipbox.com MaxDevone

    I feel bad for CodeIgniter, as its nothing to sneeze at. I’m a complete beginner to Frameworks/package since coding from scratch all these while. Any idea which is more beginner-friendly?