Development and Managed Hosting

A brief look at Laravel

Written on January 9, 2015 by Harald Nezbeda

PHP That Doesn’t Hurt. Code Happy & Enjoy The Fresh Air.”

That’s what the website says, and to be honest, it’s a framework to get the job done right and quick.

The framework itself is structured in a MVC pattern (if you have used any other PHP frameworks before you will easily get rid of using it) and it’s build by using components.

In terms of requirements you need a web server running PHP 5.4 and the MCrypt PHP Extension.

The installation is simple and uses composer for dependency management. By running the code below in your terminal you will grab a fresh copy of Laravel on your local machine.

composer create-project laravel/laravel your-project-name

After that you are ready to go. Make sure you point your host to the public folder of the framework. I would warmly recommend you using homestead for your local development environment.

Let’s have a look on some of the goodies that Laravel is offering to you:

  1. Documentation

I had to start with it because this is what actually leads to great, easy and maintainable projects, as most say Documentation is your Friend. As good as a Software may be, especially a framework, which nobody is able to use unless it is well documented or has weird “magic” is going around behind the scenes.

Luckily Laravel has a very good documentation and you can find all you need on

  1. Routing

That’s one of the parts that attracted me the most. Routes are defined in the app/routes.php file. They can be very simple, like a static path, or complex like a regular expression containing parameters. By using translations you can also localize the Routes of your application.

This is how a basic route looks like, which fires the loadPolls method from the PollsController:

Route::get('/polls', 'PollsController@loadPolls');

If you would like to localize the Route you have to add a name in the definition (such as Polls) so you can latter reference the route. This is something you should always do. The path will be taken from the translations using the Lang::get method.

        'uses' =>'PollsController@loadPolls'
  1. Blade Templates

Blade is a template language created by the Laravel developers. Main features are the easy to read syntax and an enhanced structure using base templates and sections. A base template defines a section which can be later overwritten or extended by a child template that makes use of it. The depth of this pattern can be as long as required.

In order to use blade templates you must name your views like name.blade.php.

	<h1>{{{ Lang::get('poll.list_of_polls') }}}</h1>
		@foreach ($polls as $poll)
				<a href="{{{ route('poll_detail', array( 'id' => $poll->id, )) }}}">
  1. Forms and Macros

The form builder is also dedicated to be used inside your views. Using its simple API you can dynamically generate the HTML code of your Form. Filling the form fields with existing data, as well as escaping values (to avoid Cross-Site-Scripting attacks) is being done by Laravel. You can also build custom form elements using macros.

The main advantage of the form helper is the form validation afterwards. With a few lines of code you can simply validate user input and, if needed, you can return custom error messages.

  1. Schema builder, Migrations and Eloquent

The schema builder and the Eloquent ORM will help you at creating the connection between PHP and a Database. Commonly you don’t have to write any SQL statements as this is being done by Laravel using suitable ORM functions. This way the resulting PHP application is independent of a specific database – MySQL can be easily replaced by PostreSQL without any code adjustments.

Using Migrations database schema changes can be made, such as the creation of tables and columns.

Schema::create('polls', function($table)
    $table->engine = 'InnoDB';
  1. Artisan commands

The power of the console is in your hand. Artisan commands run in the context of the framework, so you can make use of all available function (like the Eloquent ORM). Laravel comes with a list of built-in commands, which can help you switching the website into maintenance mode and more. Of course you can create custom Artisan commands which will make your development definitely easier.

  1. Great Community

The documentation is important but not the whole story. Often you need real people to share your problems with so you can find a solution together. On you’ll find a great and helpful community. Also you will find there the Laravel Podcasts, live chats and so on.