The Right Tool for the Job
The phone rings. You pick it up. It’s the national lottery — they tell you that if you can be at their office within 15 minutes, you get the entire pot, upwards of 15 million dollars!
But you’re at home, not so close to that office. You need to decide on the spot how to get there. Right now, in the middle of rush hour, going by car may take more than 15 minutes. But riding your bike might also take more than 15 minutes. You have now 30 seconds to decide and your heart is racing, because the phone call was real and he was serious. What will you choose?
That’s the lottery. 🙂
Now at work, your boss demands that the new site build start immediately and you must decide which framework to use. Probably you have more than 30 seconds to decide, but on some level, the pressure may be as high to decide and make the right decision.
This is the issue we are going to examine today — what is the right tool for the job? A car and a bike are very different tools, but each one has its place. Same for a hammer and a screwdriver — each works great for what it’s made for, and not very well for other things.
Regarding our field, PHP web development, the available tools are a bit more complicated than a hammer or screwdriver and it’s usually far less clear which is the right tool.
In the PHP framework marketplace, there are a lot options, including (more or less in order of popularity) Laravel, Symfony, CodeIgniter, CakePHP, Yii, Silex, Zend Framework, Phalcon, Slim Framework, Nette, FuelPHP, Kohana, li3, PHPixie, Fat Free Framework, Aura, PureMVC, PRADO, Nova Framework (formerly Simple MVC Framework) and Flight.
Over the past few years, Laravel has taken over the field of PHP frameworks, with today a market share of around 3 times that of Symfony or CodeIgniter. Symfony, however, is not strictly a MVC framework like Laravel is. Symfony can be used as a framework, but it’s strength is as a set of decoupled components, which can be used independently, and Laravel itself actually uses several Symfony components.
But the fact remains that the most common question asked in the PHP framework world is what are the differences between Laravel and Symfony?
We will below try to answer this question, in detail, and we will examine these areas:
- History – when and how did these two projects get started
- Product – what differences exist in the actual product
A Brief History of Laravel
A developer from Arkansas by the name of Taylor Otwell, back in 2011, created Laravel as an alternative to CodeIgniter and his first release was in June of 2011. He was developing in .net and also CodeIgniter and wanted something “better” than CodeIgniter so he made his own framework — only later did it become a fully qualified MVC framework. Over the course of the next 6 years, the project developed and grew and core components such as Blade, Eloquent ORM, Artisan, Bundles, LTS, Scheduler, Dusk and others and including a complete rewrite of the entire project in 2013 which incorporated the dependency manager Composer.
The current version is 5.4 and today Laravel calls itself “The PHP Framework For Web Artisans” and boasts a flourishing ecosystem — see below for more details. The name Laravel is a trademark of Taylor Otwell.
The Laravel framework itself is open-sourced software licensed under the MIT license.
A Brief History of Symfony
In 2004 a French developer by the name of Fabien Potencier created the Sensio Framework to help SensioLabs (his company) develop better websites faster. After he built his first website with it, the decision was made to release it as open source code under the name Symfony.
The first stable Symfony version, however, was released in January 2007. Version 2 was then released in July 2011 and was a rewrite of the entire framework from scratch.
The current version is 3.4 and is supported until 2020.
Symfony is officially sponsored by SensioLabs, a French software developer and service provider. Over 1700 people have contributed to the code base and over 1400 have contributed to the Symfony documentation, however. The name Symfony is a trademark of Fabien Potencier.
Symfony is released under the MIT license.
Laravel is an MVC framework for PHP. As they put it:
Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as authentication, routing, sessions, and caching. Laravel aims to make the development process a pleasing one for the developer without sacrificing application functionality.
What is Symfony? As they put it:
Symfony is a set of reusable PHP components and a PHP framework for web projects.
and they also say:
Symfony is a set of PHP Components, a Web Application framework, a Philosophy, and a Community — all working together in harmony.
Symfony is not actually an MVC framework, however, like Laravel. This is one of the main reasons why comparing Symfony and Laravel is not an issue of which is better, rather which is best for any particular job.
The community around Symfony is a “passionate group of over 300,000 developers from more than 120 countries” and their original forum is now read-only and current forum activity is on StackOverflow with over 50K questions tagged as Symfony questions.
Server Requirements for Laravel
- PHP >= 5.6.4
- OpenSSL PHP Extension
- PDO PHP Extension
- Mbstring PHP Extension
- Tokenizer PHP Extension
- XML PHP Extension
Server Requirements for Symfony
- PHP >= 5.5.9
- JSON Extension
- ctype Extension
- Your php.ini needs to have the date.timezone setting
Note that Laravel requires Composer and not all hosts can support that.
To install Laravel, the easiest way is via Composer, by first downloading Laravel:
composer global require "laravel/installer"
and then making a new Laravel installation with this Composer command:
composer create-project --prefer-dist laravel/laravel blog
Either of which will create a new Laravel install in a directory called blog. That’s the basic version. For more details, see our guide on How to Install Laravel on Shared Hosting
For Symfony, there are several ways to install. Some are global and some are meant only for development. We will show here one way, probably the simplest one, which is also via Composer:
composer create-project symfony/framework-standard-edition symfony
You must then answer a set of questions, but at the end, this creates a new install of Symfony in a directory called symfony.
For both Laravel and Symfony, an “install” means a set of directories which are not meant to be public. After installing the system, you must set your webroot to be blog/public/ for our Laravel example and symfony/web/ for our Symfony example.
Installation on Shared Hosting
The Composer-based install procedures listed above can work on shared hosting. The host must of course support Composer (you may have to install that yourself) and allow ssh access, and support the other requirements, but anyhow, with a good host, it works. 🙂
Under the Hood
All the above was the broad overview. The real deal, however, is what each package offers a developer, and how they work. The consensus among many developers is that Symfony is more low-level and more complicated, but also more stable and scalable and is thus best for large, involved projects. Laravel, on the other hand, is simpler and easier to use and to learn, development speed can be faster, and it’s great for simpler projects that don’t need to scale too much.
ORM means Object-Relational Mapping and it’s a system that lets you query and manipulate data without knowing what database system you are using, meaning MySQL, Postgres or SQL Server etc. The ORM provides its own high-level methods to access the data using code that is not unique to any one database system.
Some developers claim that “Eloquent is the biggest strength of Laravel.” On the other hand, it uses the Active Record pattern, which is great for small projects, but has received criticism that it won’t work for larger projects. It’s important to note that you can also use Doctrine in Laravel instead of Eloquent via the Laravel Doctrine project, which they call:
A drop-in Doctrine ORM 2 implementation for Laravel 5+
So whereas Eloquent and Doctrine are quite different in many ways, you’re not stuck with Eloquent in Laravel. It’s also worth noting that Doctrine is used in other frameworks, as well, including CodeIgniter, Zend Framework and Yii.
Laravel defines Blade as:
Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.
Twig allows you to write concise, readable templates that are more friendly to web designers and, in several ways, more powerful than PHP templates.
That’s nice — they both like their own products. 🙂
But again here, you are not stuck. You can use Twig in Laravel via the TwigBridge project which they claim:
Allows you to use Twig seamlessly in Laravel 5.
The point here is that Blade and Twig are both good tools and each has it’s advantages — these quotes are not propaganda, they’re true. 🙂
The choice of Twig vs Blade for many developers boils down to two points. One is that you can use regular old PHP code in Blade. Some folks love that because it’s easy and others claim that it encourages developers to mix business logic with presentation, whereas Twig solves these problems. Sounds a lot like what I wrote initially that Laravel is easier and faster and great for small jobs, and Symfony is harder, but has what you need for more serious jobs.
The other issue is portability — Twig is used in other projects as well and knowing it is an advantage.
The main difference here seems to be that Laravel uses PHP and JSON files to store localization data and Symfony “supports lots of different translation formats: PHP, Qt, .po, .mo, JSON, CSV, INI, etc.” and supports more standardized formats such as XLIFF and gettext.
A route is a map from a URL to a controller, meaning when a visitor to my site visits the “/dogshow” page, a route in my code directs the system to get the content for that page from the correct controller.
The Laravel Routing system works in simple PHP and the Symfony Routing system lets you define routing in “annotation” format or in YAML, XML or PHP. That again may be the main real difference, as both have a set of advanced features which cover most use cases.
Scaffolding means having a tool generate for you automatically the basic code to CRUD (create, read, update and delete) from your database, effectively treating the templates as a “scaffold” on which to build the application.
Laravel doesn’t natively have a scaffolding tool but several can be integrated into Laravel to accomplish this, such as Yeoman via the generator-laravel package, or InfyOm Laravel Generator or the Scaffold generator for Laravel 5.x.
Symfony comes with a bundle called SensioGeneratorBundle which “provides commands for scaffolding bundles, forms, controllers and even CRUD-based backends” but you can also use the generator-symfony package or the generator-sf package which uses Yeoman to “scaffold out a Symfony PHP app including Browsersync, various CSS preprocessors, jspm, webpack, browserify and Service Worker.”
Given all the various options for each, I certainly wouldn’t base a choice of framework on this issue.
There are other topics on this subject that we haven’t covered, such as Forms and Validation, Configuration, Artisan, the CLI tool for Laravel vs the The Console Component for Symfony, Lumen, the “stunningly fast micro-framework by Laravel” vs Silex a “PHP microframework built on the shoulders of Symfony and Pimple and also inspired by sinatra” and more topics as well.
As noted above, Symfony is generally for larger projects and Laravel for smaller ones, but at the end of the day of course you must use whatever you personally prefer or feel more comfortable with.
Hopefully our review can provide some insight as to the style of each tool and help you find what’s right for you.