From Laravel to Symfony — #1 Installation & Development Server

Why switching?

Switching frameworks ain’t easy

In this series I am going to share my process of switching from one PHP framework to another. I’ve been using Laravel since 2013 but had been recently offered a job at company using Symfony/Silex. Your reasons may be different, however, I wish my thoughts will provide at least some useful information.

I am by no means an expert in Symfony or Laravel, so if you’ll spot some unforgivable mistake- feel free to point it out in the comments!

I describe my attempt to learn Symfony while already using Laravel. However, if you’re switching the other way around- this series should also be helpful in some way.

Installation via composer

Both Laravel and Symfony can be installed via a simple composer command. First, let’s install the newest version of Laravel. Head to your projects’ directory and run the following command in the terminal:

composer create-project laravel/laravel laravel-demo

This will create a new folder called laravel-demo, install all necessary dependencies and run post installation scripts. Same goes for Symfony 4 project:

composer create-project symfony/skeleton symfony-demo

As you’d expect- composer will put project files inside symfony-demo and, of course, install dependencies.

It is worth noting that Symfony’s post install scripts will automatically initialize the repository (with Laravel you’ll have to run git init manually).

Dedicated installers

Apart from composer installation you can also use dedicated installers that will simplify the process. In Laravel it is simple as requiring a global composer package:

composer global require "laravel/installer"

If your $PATH variable contains composer binaries directory, you will be able to create new Laravel projects with just:

laravel new laravel-project

Symfony 4, however, does not have a working installer at the time of writing this article. There is a github package https://github.com/symfony/symfony-installer but it states:

This installer is not compatible with Symfony 4 and newer versions. Instead, use Composer and create your Symfony 4 project […]

I guess the tool was used by previous versions of Symfony but since I’ve just started using it that’s all I know. Of course, the composer installation is so simple that in most cases the installer is an overkill.

Running development servers

There are many ways to run a PHP project on your development machine. One could just install PHP and nginx or apache locally, or use built-in PHP server:

php -S localhost:8888 -t public

Then it would be accessible in the browser at http://localhost:8888. Since Laravel 5 and Symfony 4 use the /public directory as a web root this command works for both.

Another option is to use Docker or something like Laravel Valet (Don’t let the name fool you- it works for Symfony and many other frameworks. You can also write your own drivers.)

Both frameworks provide a command to run a server. In Laravel:

php artisan serve

By default you will access the site at http://127.0.0.1:8000 (but you can customize the port with an argument).

Since Symfony 4 comes very minimalistic by default, you will first need to install the “server” package (composer require server) and then run:

bin/console server:run

to start the server at http://127.0.0.1:8000.

Comparison

Creating a new package is almost indentical in both cases so there’s really nothing new to learn. The only difference is the CLI tool (Laravel has artisan at the root of the project and Symfony has console inside /bin directory) but since most developers use Docker or nginx/apache- the process you’re used to should stay the same.

The real differences are yet to be revealed so stay tuned for next articles as I’ll create a demo app with features I use in everyday work (TDD, building APIs, deployment etc.).

Part 2: Routing and controllers