Knowing Laravel: the framework that revolutionized PHP

image

If you’re still there after reading the title means you have not lost hope of finding a good PHP Framework.

The discussion between “framework yes” or “no framework” is long and I think the stronger fundamentals of the detractors are based on the options available today. But at some point we feel the need not to reinvent the wheel, especially when we have to implement joint actions (friendly URLs, session management, database management, etc).

That’s why we want to talk about Laravel, the framework that people has been taking a lot lately, taking strength in the last months and with the release of its version number 4 was positioned as a very interesting option to explore.

In this post we review the strengths and in subsequent notes I’ll show some practical examples of how to use it and the benefits it has.

The basics

The framework already has what we imagine: Router, Models, Layouts, Views, Controllers, etc. For templates it uses a proprietary engine called Blade, which has some fancy helpers.

Artisan

A client console that allows you to execute specific framework commands. It is versatile, powerful and even extends to allow us to create our own tasks so that they are available at any time from this client.

Composer

Since the last version, 4, is available directly from Composer the new package manager and PHP dependencies. This allows us to modify and add the packages we want even allowing to generate our own packages, configure them in the composer.json and include them in our application with a composer update. Such is the use and benefits of Composer that Laravel uses many other packages frameworks like Symfony (Artisan is an extension of its console) among others.

Migrations

By now we should all use a version control system for our code (and if we do not, it’s time). What Laravel incorporates is the ability to keep track of versions also of our database. This, combined with a seeding system allows us to have our application downloaded and running in a few commands. Example:

$ Git clone https://github.com/laravel/laravel.git
$ Composer update 

And to configure the database:

$ Php artisan migrate
$ Php artisan db: seed 

Ready! we have our application downloaded, installed and with its database configured.

Resources

From this latest version, the concept of resource that let us schedule our controllers as if they were a Rest API that are consumed in the same way, is incorporated. That is, to add an element we do a POST /resource to get listed a GET /resource to get an item GET /resource/{id} , etc. This allows us to release our application and consume it as an API without changing anything, and enabling us to integrate it and to be integrated with other systems as well.

Eloquent ORM

Object-Relational Mapping for our database. It allows us to interact with our database as if each table were a model, faithfully respecting the MVC division. It is very easy to use, and above all it handles a very expressive syntax for easy reading and understanding.

Generators

One of the things that I like is the time they save. They are based on a package created by Jeffrey Way that can be added to composer.json to automatically download it at the start of our app’s development. What we need to do is add it as required:

  "Require": {
     "Laravel / framework": "4.0 *."
     "Way / generators": "dev-master"
 } 

After a composer update we’re ready to use it. This will allow us to generate a template of any of the items we need, whether it be a Model, Controller, Seed, View, Migration, etc.

A practical example to understand the comfort of using it: let’s create a resource to manage the users of our application:

$ Php artisan generate: --fields resource user = "name: string, name: string, username: string, password: string" 

The result of the command creates the following:

  • Views to the list, detail, edit, and sign ups
  • General Layout (it creates it if is the first time you run it)
  • Model Resource (User)
  • Controller Resource with all its methods
  • Migration to create the table with all its management methods.
  • Modifies the file routes.php (containing mapping of all routes) to add the Resource
  • Creates the class to do the Unit Testing.

In short, once you’re done, you can access http://localhost/usuarios and we will have a fully functional ABM to extend it and / or modify the way we want. This is an excellent way to shorten the initial development time especially when we need to test the implementation quickly to make better decisions.

And much more

There were many interesting things out, as it comes with a driver to use Redis storage, support for Unit Testing, Validations, etc. This is just a sampling of some of the features. You can see the rest of the magic in the official documentation .

In the coming weeks we will develop our first application using Laravel.