My top 5 favorite symfony packages
Tags: symfony, packages, recipes, webdev
Published: 2021-12-27 14:02:47

As I told you earlier in my previous blog [Starting a new Symfony project on Linux](https://dev.to/eelcoverbrugge/starting-a-new-symfony-project-on-linux-2amh), Symfony is all about start small and add features when you need them. In this blog I'll explain how to install packages and what my top 5 favorites are.

## Install packages

Packages are installed via Composer:
```
composer require [package_name]
```

Symfony however calls them recipes and are also installed via Composer but differently:
```
composer recipes [recipe_name]
```

Check out https://flex.symfony.com/ for Symfony's recipes.

### 1. Annotation support
Annotations are meta-data not only used by your IDE, but also by Symfony. For example by using routes:
```
use Symfony\Component\Routing\Annotation\Route;

class AwesomeController
{
    /**
    * @Route("/awesome")
    */
    public function awesome()
    {
        //...do something
    }
}
```

If you like to add your routes via annotations like above, make sure you install the annotations package:

```
$ composer require annotations
```


### 2. Twig template
Twig is a template engine for the PHP programming language and loved by Symfony. For example like this:

```
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Awesome!{% endblock %}</title>
        {% block stylesheets %}
            <link rel="stylesheet" href="/css/style.css">
        {% endblock %}
    </head>
    <body>
        <a href="/"><img src="/images/logo.png"></a>
        {% block body %}{% endblock %}
        {% block javascript %}{% endblock %}
    </body>
</html>
```

In order to use Twig, install the template package:

```
$ composer require template
```


### 3. The Asset Component
To manage URL generation and versioning of web assets I like to use:
```
composer require symfony/asset
```

This will generate urls and handle versioning if you replace this:
```
<link rel="stylesheet" href="/css/style.css">
```
```
<a href="/"><img src="/images/logo.png"></a>
```

with:
```
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
```
```
<a href="/"><img src="{{ asset('images/logo.png') }}"></a>
```

### 4. Web Debug Toolbar
The profiler is a powerful development tool that gives detailed information about the execution of any request.

By using the '--dev' attribute, your debug toolbar is only visible and used in your dev environment so it will never pop-up in production.

Use the toolbaar by installing the profiler package: 
```
$ composer require profiler --dev
```

Now if ever using dump() or dd(), notice your dump will not show on top in the browser anymore but in the toolbar:
```
use Symfony\Component\Routing\Annotation\Route;

class awesomeController
{
    /**
    * @Route("/awesome/{id}")
    */
    public function awesome($id)
    {
        dump($id);
        dd($id);
    }
}
```

This is just the tip of the iceberg of what the toolbar can do. Check out Symfony's page to read more about: https://symfony.com/doc/current/profiler.html 


### 5. Symfony's Maker bundle
Now here the magic really starts. Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

Run this command to install:
```
$ composer require --dev symfony/maker-bundle
```

To get an overview of all commands, simply use:
```
$ php bin/console list make
```

Try running 'php bin/console [make_command]' and add ' --help' to see what the options are:
```
$ php bin/console make:controller --help
```

It will exactly explain how you can generate out-of-the-box code. For example:
```
$ php bin/console make:controller CoolStuffController
```

Awesome! It created this controller in less then a second:
```
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class CoolStuffController extends AbstractController
{
    /**
     * @Route("/cool/stuff", name="cool_stuff")
     */
    public function index(): Response
    {
        return $this->json([
            'message' => 'Welcome to your new controller!',
            'path' => 'src/Controller/CoolStuffController.php',
        ]);
    }
}
```

### Uninstall packages/recipes

If ever needed, you can uninstall packages/recipes by the following command:
```
composer remove [package_name_or_recipe_name]
```

## Let me know
What packages do you use from the start of a new project?