Меню

Key differences between Laravel and Symfony

logo
Which of two frameworks Laravel or Symfony is better to choose for the implementation of the project? For what specific needs this or that framework should be used? Description of the functionality of the frameworks
Key differences between Laravel and Symfony

Symfony or Laravel

In the era of the existence of a large number of PHP frameworks, the PHP developer is often faced with the question: through which framework the project should be implemented?
Many people have already made their choice, finding the most convenient for themselves. However, for some people, it is difficult to make a decision.
Let's take a look at and compare some of the functionality of such popular frameworks as Symfony and Laravel.

Installation

The core files of modern PHP-frameworks can be installed using the package manager composer.

So, for Laravel, this can be done with the command create-project --prefer-dist laravel/laravel project_name for Symfony composer composer create-project symfony/website-skeleton project_name, where project_name is the name of the project and at the same time the name of the directory to be created, in which composer will place the framework files.

At the same time, both systems now have their own installer, laravel new project_name and symfony new project_name, and the installers themselves can be installed by executing one line: composer global require laravel/installer and wget https://get.symfony.com/cli/installer -O - | bash respectively.

These data indicate that the process of installing the minimum functionality is minimized by the intervention of the developer and takes very little time.

Setup

Both frameworks now support the configuration.env-file, which stores key-value pairs, but in addition, Symfony also supports yaml files for configuring routing, services, and more.
The yaml format is very easy to use and allows specifying the values of the configuration parameters in a form that resembles a multidimensional array

Routing

The list of routes of the application and binding to controllers in each of the frameworks can be set in its own way. So, to indicate in Laravel that the route "/test" should be processed by the test method of the TestController controller, the following Route::get('/test', 'TestController@test')->name('test_name') should be specified in the routing file routes/api.php;
For Symfony, the same can be done using routing yaml files, or using annotations, which is very convenien

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class TestController {

    /**
     * @Route("/test", name="test_name")
     */
    public function test() {
         return new Response('Some test text');
    }
}

Using controllers has a huge number of advantages. So, dynamic parameters can be set in the route path "/test", for example, "/test/{id}". Now the parameter $id should be specified in the controller signature, and its value will be pulled out of the way. In addition, dynamic parameters can be set using regular expressions, so that, for example, $id is only an integer. As well, each framework has the functionality to combine routes into groups and assign prefixes to them, assign HTTP methods by which these routes will be available, and provides convenient support for working with GET- and POST-parameters.

Migration and ORM 

Both systems offer a convenient set of console commands for working with project migrations, but perhaps the key difference between Laravel and Symfony consists in working with them. In Laravel, we are forced to create migrations on our own, and we have to monitor the synchronization of the database structure and PHP classes on our own.

So, for example, the migration code will look like this:

Schema::create('tests', function (Blueprint $table) {
    $table->bigIncrements('id');
});

and the “Test” class will look like this:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Test extends Model
{
    protected $table = 'tests';
}

We see that all models in Laravel inherit the “Model” class.

In Symfony, migrations are handled through Doctrine ORM. Here, we can only work with PHP classes and annotations.

The sample of the code for the “Test” class will look like this:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Test
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;
}

fter the Test entity has created, we can start the migration creation, where Symfony will create the executable SQL code on its own for creating the “tests” table with the id column. Moreover, we do not need to carefully monitor the synchronization of the structure of the database and PHP classes, because when creating migrations, the framework compares the current database structure and the expected one based on the current PHP classes and creates all the necessary SQL.

It is worth noting that for the samples from the database, ORMs are provided in both frameworks. In Laravel, Eloquent is responsible for this. For example, the code below allows getting a collection of objects of the “Test” class:

$tests = App\Test::where('id', '<', 100)
   ->orderBy('id', 'desc')
               ->take(10)
               ->get();

In Symfony, such a frequently used code is taken out to special classes - repositories, and the body of the method itself would look like this:

$tests = $this->createQueryBuilder('t')
            ->where('t.id < :id')
            ->setParameter('id', 100)
            ->orderBy('t.id', 'DESC')
            ->setMaxResults(10)
            ->getQuery()
            ->execute();

In this case, an array of objects of the “Test” class will be returned, and the Doctrine ORM will be responsible for this.
Both Eloquent and Doctrine implement the Fluent Interface design pattern, which can be seen from the names of the “where”, “orderBy” methods, and other ones.

Template engines

Out of box, Symfony works with Twig, and Laravel works with Blade.
Their functionality is very similar, only the syntax is slightly different. Template engines support overriding of blocks, file inheritance, loops, and much more.
Best of all, the difference will be seen from the examples of the code.

Twig:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Test Application{% endblock %}</title>
    </head>
    <body>
        <div id="sidebar">
            {% block sidebar %}
                <ul>
                    <li><a href="/">Home</a></li>
                    <li><a href="/blog">Blog</a></li>
                </ul>
            {% endblock %}
        </div>

        <div id="content">
            {% block content %}{% endblock %}
        </div>
    </body>
</html>

Blade:

<!DOCTYPE html>
<html>
    <head>
    <meta charset="UTF-8">
        <title>@yield('title')</title>
    </head>
    <body>
    <div id="sidebar">
        @section('sidebar')
            <ul>
                <li><a href="/">Home</a></li>
                <li><a href="/blog">Blog</a></li>
            </ul>
        @show
        </div>

        <div id="content">
            @yield('content')
        </div>
    </body>
</html>

If you are familiar with one of the template engines, then you will deal with the other without any problems.
As for the front end, Laravel also provides out-of-box Vue.js support, which opens up additional possibilities, but in the modern web, it is not impossible to use any PHP back end framework in combination with a JavaScript front end framework such as React, Angular, or Vue.js.
As a conclusion, we can say that it is difficult to distinguish the best one between two large frameworks Laravel and Symfony, but only the one that is more suitable for specific needs. Although they implement different strategies for creating the resulting web application, none of the implementations can be called bad. Both frameworks allow significantly extending the basic functionality for your needs, and a huge number of packages installed by means of the composer only help with this. Both Laravel and Symfony can be used both for small sites and for corporate ones, including due to built-in caching systems and high performance. Each of them is potentially well extensible, so after you have chosen the framework for a new project, further extensibility is up to you.

Tips for connecting  the ShipStation API serviceto 17.09.2019 Tips for connecting the ShipStation API serviceto
Use the ShipStation API as an aggregator for your service and as a great tool for saving money on sending packages. Description of the service’s features

Paypal API Connection Features 02.10.2019 Paypal API Connection Features
In this article we will consider the REST API, which allows to conveniently interact with all the Paypal entities.

Back to the list