Skip to main content
Image for post: Getting started with Laravel 5

Getting started with Laravel 5

Setup, tools and configurations

I like PHP MVC frameworks and now Laravel has all my curiosity. It's fast, modern, clean, more lightweight and with good documentation both in the code and on the official website. Built using Symfony and the Symfony components, Laravel has tools and libraries to work even more efficiently with all modern features like differents database, different file storage systems and caching.

Requirements

  • PHP >= 5.5.9
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension

Installation

Following the procedure on the official website:


sudo composer create-project laravel/laravel learning-laravel-5 dev-develop

NOTE: this will install the new and dev version of Laravel. I will NOT recommend this version for now because you can have problems using some very useful dependencies available with composer. Run a server: php -S localhost:8888 -t public After the installation I've got a blank page instead of having the page with the Laravel 5 lavel. If you will have the same problem, here is the solution (I'm using Linux):


sudo chmod -R o+w storage/

That's because Laravel needs to write into the storage directory. If you will have an error with "storage permission denied" message here is another solution:


php artisan cache:clear
chmod -R 777 framework/
composer dump-autoload

Artisan

The Laravel command line tool is called Artisan. We will use it a lot and like the Symfony console, it has many useful features and it can be extended. Type php artisan to see the whole command list.

Homestead

Here we have a Virtual Machine with Laravel running on Vagrant. I admit this is not simple to install and use for the first time. Here is the github and the documentation.

Routes

The routes are in the app/Http/routes.php file. The command php artisan route:list shows the route list. The are different ways to define routes and you will find this very interesting because Laravel allows us to many things. The documentation is pretty clear.

MVC workflow

With Artisan you can create a controller. The following command will create a controller with all REST and CRUD methods: php artisan make:controller UsersController If you want a simple class with only the controller name you will simply type php artisan make:controller UsersController --plain Controllers are in app/Http/Controllers directory. Now you will define a route, create a blade template view on resources/views directory and you will be able to run a web page.

Database

You can access the Query Builder directly (it's very similar to the Doctrine DQL language but with some more interesting features) or use the Eloquent ORM. Don't worry about learning another ORM, both Eloquent and the query builder are easy if you are familiar with Doctrine or not. With the query builder, you can build your own query using this object, exactly like you can do with DQL. Using the Eloquent ORM, you can extend a Model. First you create a Model (for example Users):


php artisan make:model Users

The next step

It's difficult to describe a big framework like Laravel in a single post. I will write more specific posts. I followed a course on Laravel 5 and I'm very happy with this technology. Laravel 5 is modern, fast, lightweight and it borrows from big frameworks like Sinatra or ASP NET MVC5. Working with Eloquent is amazing. It will help you to do complex operations and Laravel itself will allow you to do them keeping the code short, clean and readable. The other frameworks do not allow you to do this and an ORM like Doctrine is hard to understand and learn even if it can be very helpful at the end. I can spend the same words for a framework like Zend. Symfony will continue to be one of the best PHP framework ever but for me Laravel is the first alternative. So see the Laravel cheatsheet and save this page because it's very helpful and start working with Laravel 5 ASAP if you can if possible. Here is my example application built with Laravel 5. Here I show some tricks like working adding new interesting features like breadcrumb. baum for a nested tree category, working with forms, database relationships and more...

Share this post

This website and/or any sub domains use cookies to understand how you to improve your experience.