How to set up and configure Symfony

Updated: January 13, 2024 By: Guest Contributor Post a comment

Introduction

Symfony is a highly popular PHP framework designed for developers who need a simple and elegant toolkit to create full-featured web applications. This guide will walk you through setting up and configuring a new Symfony project, including some basic and advanced configurations.

Installing Symfony

Before you begin, ensure you have Composer installed, which is a dependency manager for PHP. With Composer ready, you can install Symfony using the following command:

composer create-project symfony/website-skeleton my_project_name

This command will set up a new Symfony application in a directory named my_project_name. The website-skeleton distribution comes with a pre-configured setup that is suitable for most applications with a user interface.

Understanding Symfony’s Structure

Once installed, take a moment to familiarize yourself with Symfony’s directory structure:

  • /assets: Stores your front-end files like JavaScript and CSS.
  • /bin: Contains Symfony’s executables.
  • /config: Holds all your configuration files.
  • /public: The web root of your project where your front controller lives.
  • /src: Houses your PHP code including controllers and domain code.
  • /templates: Where the Twig template files are located.
  • /translations: Contains translation files for multi-language support.
  • /var: For logs and cached files.
  • /vendor: Composer dependencies are installed here.

With the basic understanding of the project layout, you can dive into the configurations.

Configuring the .env File

Environmental configurations such as database connections strings are located in the .env file at the root of your project. Duplicate the .env file and name the duplicate .env.local to override settings without affecting version control:

# .env.local
DATABASE_URL=mysql://db_user:[email protected]:3306/db_name

Creating a Database

With the database connection configured, create the database using the Symfony console command:

php bin/console doctrine:database:create

Check the output to ensure the database is created successfully.

Creating an Entity

An entity represents a table in your database. Create an entity Player for instance:

php bin/console make:entity Player

After answering a few questions, your entity is created with an associated PHP class came.

Creating a Controller

Controllers manage the flow of the application execution. Use the following command to create a controller:

php bin/console make:controller PlayerController

This action creates a new PlayerController class within src/Controller/ folder with a default index method.

Routing Configuration

Routes define URLs for your application. You can configure routes in the config/routes.yaml file or above the controller’s method using annotations:

/
# src/Controller/PlayerController.php
/**
 * @Route("/player", name="player_index")
 */
public function index(): Response
{
 // ...
}

Advanced Configuration: Creating a Service

As you become more comfortable with Symfony, you will encounter situations which require advanced configurations such as custom services, event listeners, or security. Let’s touch on creating a service.

Services are classes where you put logic that doesn’t fit into an entity or controller. First, create your service class:

namespace App\Service;

class PlayerManager
{
    public function create() 
    {
        // Your logic here
    }
}

Then, you can define it in config/services.yaml to make it available for dependency injection:

services:
    App\Service\PlayerManager:
        # you can configure your service here

You can now inject this service into controllers or other services.

Testing Your Application

It’s vital to ensure your application works as expected. Symfony has a built-in PHPUnit Bridge, you can run the following command to execute tests:

php bin/phpunit

Conclusion

This guide has outlined the necessary steps to set up and configure a Symfony project from installing the initial project to advanced configurations like service creation and running tests. With this knowledge, you are well on your way to developing robust PHP applications using the Symfony framework. Remember, the Symfony documentation is an invaluable resource as you expand your Symfony skills.