DEV Community

Eelco Verbrugge
Eelco Verbrugge

Posted on

My top 5 favorite symfony packages

As I told you earlier in my previous blog Starting a new Symfony project on Linux, 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]
Enter fullscreen mode Exit fullscreen mode

Symfony however calls them recipes and are also installed via Composer but differently:

composer recipes [recipe_name]
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

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

$ composer require annotations
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

In order to use Twig, install the template package:

$ composer require template
Enter fullscreen mode Exit fullscreen mode

3. The Asset Component

To manage URL generation and versioning of web assets I like to use:

composer require symfony/asset
Enter fullscreen mode Exit fullscreen mode

This will generate urls and handle versioning if you replace this:

<link rel="stylesheet" href="/css/style.css">
Enter fullscreen mode Exit fullscreen mode
<a href="/"><img src="/images/logo.png"></a>
Enter fullscreen mode Exit fullscreen mode

with:

<link rel="stylesheet" href="{{ asset('css/app.css') }}">
Enter fullscreen mode Exit fullscreen mode
<a href="/"><img src="{{ asset('images/logo.png') }}"></a>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To get an overview of all commands, simply use:

$ php bin/console list make
Enter fullscreen mode Exit fullscreen mode

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

$ php bin/console make:controller --help
Enter fullscreen mode Exit fullscreen mode

It will exactly explain how you can generate out-of-the-box code. For example:

$ php bin/console make:controller CoolStuffController
Enter fullscreen mode Exit fullscreen mode

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',
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Uninstall packages/recipes

If ever needed, you can uninstall packages/recipes by the following command:

composer remove [package_name_or_recipe_name]
Enter fullscreen mode Exit fullscreen mode

Let me know

What packages do you use from the start of a new project?

Top comments (0)