15/04/2014
Why I love the Laravel framework?
Long time ago ...
I wrote my own framework. It's a simple and multi-tiers, but when I write web application in this framework, it's a way to hell. A lot of people told me: try laravel, it's a very simple and nice web framework. So I had to try it out.
What is Laravel
Laravel is a clean and classy framework for PHP web development. Freeing you from spaghetti code, it helps you create wonderful applications, using simple, expressive syntax. Development should be a creative experience that you enjoy, not something that is painful. Enjoy the fresh air!
Laravel is a PHP 5.3 framework that describes itself as a “A Framework For Web Artisans”. According to its author, Taylor Otwell, Laravel strives to bring back the joy to programming by making Laravel simple, elegant, and, most importantly, well-documented.
From my experience with the framework, I would definitely agree that Laravel hits these three points dead-on:
Simple - Laravel's functionalities are easy to understand and implement. If you enjoy how simple and easy CodeIgniter is, then you'll love Laravel
Elegant - most of Laravel's functions work seamlessly with very little configuration, relying on industry-standard conventions to lessen code-bloat
Well-documented - Laravel's documentation is complete and always up-to-date. The framework creator makes it a point to update the documentation before releasing a new version, ensuring that people who are learning the framework always have the latest documentation.
What Makes Laravel Different?
As with any PHP framework, Laravel boasts a multitude of functions that differentiates it from the rest of the pack. Here are some, which I feel are the most important (Based on http://laravel.com/docs).
Bundles
Bundles are to Laravel as PEAR is to PHP. Bundles are to Laravel as PEAR is to PHP; they are add-on packages that you can download and plug into your Laravel installation. At the moment, there are quite a few bundles in the Laravel Bundle Repository, with more being added all the time. Laravel comes with a command-line tool called Artisan, which makes it incredibly easy to install bundles.
Bob the Builder
One of my favorite Laravel Bundles, called Bob the Builder adds a useful scaffolding tool to Laravel, and lets you generate different kinds of files and classes suited for Laravel, such as controllers, models, migrations and tests. This functionality is quite similar to what you might expect from Rails generators. To install Bob, simply use the Artisan command-line utility, like so:
php artisan bundle:install bob
Eloquent ORM
The Eloquent ORM is the most advanced PHP ActiveRecord implementation available. The Eloquent ORM is, by far, one of the best ORM implementations I've used. Similar to how Doctrine ORM functions, it makes any work on database records simple and easy. It abstracts most functions that you'll have on models (i.e. CRUD operations) and provides a flexible way to add more. Additionally, the Eloquent ORM gives you the ability to define model relationships to retrieve records, based on their relationship to another record. For example, you can retrieve all related file records associated with a user by doing:
foreach( $user->Files as $file ) {
echo $file->name;
}
Migrations
Database migrations are a great utility in any project's arsenal - especially for projects where multiple developers are involved - by making it easy to keep your database schema up-to-date with other team member's changes. In Laravel, migrations are built into the framework; they can be executed, via the Artisan command-line utility. Laravel's own Schema Builder functions are simple enough that anybody should be able to quickly write up a database schema change.
Here's an example, taken from the Laravel documentation:
Schema::table('users', function($table)
{
$table->create();
$table->increments('id');
$table->string('username');
$table->string('email');
$table->string('phone')->nullable();
$table->text('about');
$table->timestamps();
});
Unit-Testing
Sane Programmer from the TDD tutorial
As a believer in Test-Driven Development (Read for more info: The Newbie's Guide to Test-Driven Development), I love it when a framework has some sort of unit-testing utility baked in. Laravel's own beautifully integrates with PHPUnit, relying on its status as one of the industry's best PHP unit testing frameworks. To build a test, simply extend the PHPUnit_Framework_TestCase class, like so:
class MyUnitTest extends PHPUnit_Framework_TestCase
{
public function somethingShouldBeTrue()
{
$this->assertTrue(true);
}
}
To run your Laravel application's tests, let's, again, use the Artisan command-line utility:
php artisan test
That command will run all tests, which are found within the application/tests directory of your Laravel application.
Redis
Redis is a key-value database, similar to CouchDB and MongoDB. It's used by many web applications to store non-relational data, as opposed to conventional databases (like MySQL), which store records that usually relate to one another. Redis support in Laravel is executed so elegantly to the point that I can't even begin to describe how easy it is to get up and running.
Once you have your Redis server set up, simply open the database.php file and add your Redis configuration, like so:
'redis' => array(
'default' => array('host' => '127.0.0.1', 'port' => 6379),
'staging' => array('host' => 'redis-db-staging.host', 'port' => 6379),
'production' => array('host' => 'redis-db-production.host', 'port' => 6379),
)
Right off the bat, we can see that Laravel supports multiple Redis configurations, based on your application's environment. Once you have your Redis configuration in, you can start making calls to Redis, like so:
$redis = Redis::db(); //this gets a Redis object connected to the 'default' configuration
$redis = Redis::db('staging'); //this gets a Redis object connected to the 'staging' configuration
$redis = Redis::db('production'); //this gets a Redis object connected to the 'production' configuration
$redis->set('site', 'our site');
$site = $redis->get('site');
$sites = $redis->lrange('sites', 0, -1);
Laravel provides a thin client for us, so all Redis commands can be called on the Redis class - Laravel takes care of converting it into the proper Redis query.
First contact
We started riding with laravel and it was very enjoyable. The use of this framework is very simple. The extension of the new library also turned out to be trivial. We wrote a login mechanism in 4 hours and expanded quickly in the functionality of the application on Facebook, Twitter, Google+ and Linkedin in a few moments. Password reminder was generated in 15 minutes.
Summary
Laravel is truly an amazing framework. It's fast, simple, elegant and so easy to use. It absolutely merits being considered as the framework to be used for your next project. It is better than the illogical Yii, better than Symphony2 (laravel contains elements Symphony2), better than Zend (even impossible). I sincerely recommend it.