Codementor Events

What are the best PHP Frameworks in 2018? Symfony? Laravel? Or else ?? Let’s compare those two!

Published May 18, 2018
What are the best PHP Frameworks in 2018? Symfony? Laravel? Or else ?? Let’s compare those two!

In this post, I will try to compare most popular PHP frameworks. I know that writing posts like this could be like putting a stick in an anthill, but I will do my best to be fair for both sides 😉

When you start to learn PHP or are deciding about technology stacks for new project, sooner or later you have to make a decision and choose a right PHP framework.

The decision could be really hard because of multiple possibilities. Choosing the right framework could be crucial for your project but also for your PHP learning journey.

The Possibilities

Let's start from the possibilities. In the last of couple years, when you ask programmers about PHP frameworks, the standard answer was always Symfony. Most modern open source software was also rewritten to use more or fewer features from Symfony frameworks. But in last two years, we can observe the growing popularity of Laravel framework.

What are the other options? Well, let's focus only on those most popular ones. Besides Symfony and Laravel, we can also use Codeigniter, Zend, CakePHP, YII, Phalcon and more..

Let's see how popularity has changed in the last two years:
1.jpg

Source: https://trends.google.com/trends/explore?q=laravel,zend,codeigniter,symfony,cakephp

From this chart, we can see that there was Laravel and for a long long time, nothing.
On the second place is almost exactly Symfony and Codeigniter.

Let's look also at the number of questions regarding those frameworks on Stack Overflow:

2.jpg

I am a fan of Symfony, however, I can understand the growing popularity of Laravel Frameworks. For the purpose of this article, I will focus myself on those two frameworks only.

In my next article, I will compare other PHP frameworks.

It's all about complexity!

When you start to learn PHP and you get to the moment when it's time to learn first framework, Laravel will be the better choice for you. Why? Because it's very simple to learn.

Symfony is waaaay more complicated. But when you are an experienced programmer and about to choose technology for a new project, then your decision could be different.

If you are skilled at PHP PRO, then you simply know that Symfony rocks! The possibilities of customization are unlimited. Let's compare this to real life.

Using Laravel is like buying a laptop, and using Symfony is like building a PC from scratch, deciding what parts and components to use. Symfony is way more complexed and perhaps harder to learn. When you master it, you can see it's also way more flexible.

But is it always the best framework to use?

It all depends on the project..

When you have small projects, or are in the first phase of, for example, a startup product, Laravel could be better choice for you. It provides a simple app that you can grow very rapidly. Also, many programmers know Laravel very well so it's going to be easier to scale the project and provide more developers.

But if you are working on a more advanced project, Symfony is the best choice you can make. It's fast, flexible, expandable, stable, and sustainable. Like with Laravel, there are a lot of programmers who know Symfony and, also, there's a huge library of bundles and documentations available online.

Let's compare!

table.jpg

There's one more fact I must point out. Laravel was built on Symfony. Yes, core components like routing, HTTP, sessions and etc. are used from Symfony:

3.jpg

Laravel constructors take what's bests from Symfony and make it simplier and more rapid to use.

Pro's and Con's

Laravel Pro's:

  • Rapid development
  • Simple file and code organization
  • Great documentation
  • ORM
  • Unit testing
  • Many built in functionality
  • Artisan – great Command Line Interface
  • Solid encryption
  • Proper cache with good cleaning

Laravel Con's:

  • Too much queries to DB
  • Problems with working on shared hostings
  • Sometimes it's just slower than other frameworks

Symfony Pro's:

  • Performance, performance, performance!! (in most recent versions using PHP7!)
  • Flexibility
  • Lots of bundles, packages, commercial third party software, documentations etc.
  • ORM
  • Unit testing
  • Many extension projects for CMS like EasyAdmin, SonataProject etc.
  • Symfony profiler – great tool

Symfony Con's:

  • Complex documentation
  • Many differences between maintained versions
  • Steep learning curve

Differences in code

Now, let me show you some sample codes in Symfony and Laravel doing the same, so you can decide what suits you best.
For my examples, I will be using mostly Symfony in version 3.4. There are some differences in Symfony 4.

  1. Installation!
    In Symfony 4, you may install projects through composer. Simply use:
composer create-project symfony/website-skeleton my-project

But in the previous version of Symfony, you need to do more:

sudo mkdir -p /usr/local/bin
sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

In Laravel, it is as simply as in Symfony 4:

composer global require "laravel/installer"
laravel new laravel_project
  1. Configuration

The first thing you will probably edit after the installing project is the configuration. In Symfony, there are many configuration files but the most important is in app/config/config.yml . Symfony allows you also to create different configurations for every environment by simply using config_ENV.yml file.

Standard configuration in Symfony 3.4:

# app/config/config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

framework:
    secret:          '%secret%'
    router:          { resource: '%kernel.project_dir%/app/config/routing.yml' }
    # ...

# Twig Configuration
twig:
    debug:            '%kernel.debug%'
    strict_variables: '%kernel.debug%'

# ...

In Laravel, you have a file with keys and values .env and a app.php file with standard PHP configuration.

Standard Laravel .env file:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Standard Laravel app.php file:

<?php

return [
    'name'     => 'Laravel',
    'env'      => env('APP_ENV', 'production'),
    'debug'    => env('APP_DEBUG', false),
    'url'      => env('APP_URL', 'http://localhost'),
    'timezone' => 'UTC',
    'locale'   => 'en',
    'key' => env('APP_KEY'),
    'providers' => [
    // …
    ],
    'aliases' => [
    // …
    ],
// …
];

For me, Symfony's YAML is more readable. However, if you don't know YAML, then in Laravel, you can live with that 😉

  1. Routing

In Symfony, we mostly define routing by YAML annotations in controller. Example in Symfony 4:

// src/Controller/BlogController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

class TasksController extends Controller
{
    /**
     * Matches /tasks exactly
     *
     * @Route("/tasks", name="tasks_list")
     */
    public function list()
    {
        // ...
    }

    /**
     * Matches /tasks/*
     *
     * @Route("/tasks/{slug}", name="task_show")
     */
    public function show($slug)
    {
        // $slug will equal the dynamic part of the URL
        // e.g. at /tasks/task1, then $slug='task1'

        // ...
    }
}

In Laravel, we have default route files. That's the place where we define some routes. For example:

Route::get('/', function () {
    return view('welcome', [
        'name' => 'Michal'
    ]);
});


Route::get('/about', function() {
    return view('about');
});

Route::get('/tasks', function() {

    $tasksDB = Task::all();

   
    return view('tasks.index', compact('tasks', 'tasksDB'));
});

As again, you may have noticed that Laravel's routing is in one file and it may be easier (especially for smaller projects).

  1. Templates

Symfony uses Twig templates. It's a very good templating language where you can code front-end in readable templates that are more friendly to front-end developers, etc. Front-end developers may code templates in TWIG without knowledge of PHP.

Little example:

<!DOCTYPE html>
<html>
    <head>
        <title>Welcome to Symfony!</title>
    </head>
    <body>
        <h1>{{ page_title }}</h1>

        <ul id="tasks">
            {% for item in tasks %}
                <li><a href="{{ item.href }}">{{ item.title }}</a></li>
            {% endfor %}
        </ul>
    </body>
</html>

In Laravel, we have a PHP based blade templating system. Again, if you know PHP, you don't have to know Twig, but if you are a front-end developer, you need to have some PHP background to code templates in Blade.

Example below:

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel</title>
    </head>
    <body>
 

        <h2> TASKS: </h2>

        <ul>
        @foreach ($tasksDB as $task)

            <li>
                <a href="/tasks/{{ $task->id }}">
                    {{ $task->title }}
                </a>
            </li>

        @endforeach
        </ul>
    </body>
</html>

Which framework is better?

Personally, I think that there is no straight answer to this question. If you are working in Symfony, and you think that it's a lot better than Laravel or another way, and you are an experienced Laravel developer and you always saysthat Symfony sucks, then all I can say is that you did not understand the framework you're complaining about.

Both frameworks are great – all you need is to know which one will be better for your current project.

So what framework should you use?

In my opinion, you should pick the framework based on the project specification rather than the popularity of the framework.

As a programmer, I've working with Symfony for over five years. Since year one, I'm using also Laravel and I see the pros and cons in both of those frameworks.

The decision is up to you. I recommend you to get familiar with both of them. Then, do some projects using Laravel and some other projects using Symfony. Only experience can give you enough knowledge to choose what framework will be best for your next project.

Looking on global trends, I guess that if you want to call yourself a professional back-end developer, you need to know at least those two frameworks.

Discover and read more posts from Michal Wojtasik
get started
post commentsBe the first to share your opinion
Patrick Thurmond
5 years ago

Your article leaves a lot to be desired. Your config listing acts as though Symfony doesn’t need settings for DB, email, security, etc. It does if it wants to use those. That it divides them up into sub-files doesn’t change that.

You say that there are too many DB queries in Laravel, but based on what exactly? Do you have a poor DB design? In which case you will have that problem no matter what framework you use. The number of queries won’t go up between Symfony and Laravel. That doesn’t make any sense.

You mention shared hosting issues but I don’t see how that is possible. If it can run Symfony it can run Laravel unless you are using some special package that requires some extension. That isn’t a valid argument at all. I have never seen any hosting issues with it. It works better/faster than Wordpress or Drupal (I have worked extensively with both). Especially on local environments and in VMs.

It might be slower than some, but hardly any, other frameworks. All the benchmarks I have seen with Laravel have shown it blowing the competition away.

Laravel also has lots of bundles/packages available and can actually use a lot of what Symfony uses. That is the beauty of the package system using Composer in PHP. They also have a large and very active community of developers.

There are also a lot of extensions for integrations with CMS for Laravel as well.

I feel like this article is a lot more shallow in Laravel knowledge than it is on Symfony knowledge.

Miramar Aristide Alingo
5 years ago

Bonjour monsieur Michal. Vraiment, j’ai lu avec attention et amour votre comparaison entre Laravel et Symfony et j’ai apprécié tout le contenu. Personnellement, je code avec Symfony et j’avoue que c’est un framework exceptionnel. J’ai aussi compris beaucoup de choses par rapport à Laravel.

Je vais toujours utiliser Symfony car pour moi, c’est le meilleur et c’est adapté aux grandes entreprises. Symfony + PhpStorm = Free Life.

Tout ce qui est difficile avec Symfony, c’est la rudesse de l’apprentissage lorsqu’on est débutant.

Merci encore pour l’article.

Alex Barker
5 years ago

I personally think that Laravel is terrible. Dynamic code execution, forced ORM, convoluted workflow. As a result, many functions cannot be found by IDE’s and lots of stuff “just works” auto-magically at the expense of performance and the developers comprehension. Sure, if you need to bang out a mom-and-pop website for some 100 requests a day website, its fantastic. But if you need to scale, like at the enterprise level, this is a total trash-fire. Not to mention the TrustedProxy that is installed by default. X-Forwarded-For is not secure so stop using it in place of IP tables and web-server configuration. It freaking blew my mind that this was included by default, but then again I looked into how some of the auto-magic dynamic code execution works and literally lost my mind. I would never deploy Laravel for any serious PHP development.

Michal Wojtasik
5 years ago

As a Symfony dev i can only say “yes!” for your post! But still laravel is very popular framework…

Patrick Thurmond
5 years ago

Just so you know, the ORM is not forced. You can use it or not use it or use something else. It provides tools you can use if you want to. That is how most of the framework is. It is, after all, built on Symfony.

Igor Rebega
5 years ago

I used to work with Yii as my first framework and was happy with it, after that I changed to Yii2 and was twice that happy.

And after that, I got a project on Laravel, and on the first stages,

  • I hated him, so much magic, so much thinks that your IDE just can’t understand.

But the time comes and I started to realize how all that works, that there are great learning resources about Laravel, very big and strong community and many devs are happy with it.

So I started my Laravel learning curve and now I’m so happy about it. And did a pretty big enterprise project on it. So just give it a try and I’am sure that you will change your mind!

Show more replies