Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Comprehensive Tutorial: Using Symfony

Introduction to Symfony

Symfony is a set of reusable PHP components and a PHP framework for web projects. It speeds up the creation and maintenance of PHP web applications while replacing repetitive coding tasks.

Getting Started

To start using Symfony, you need to have PHP and Composer installed on your machine.

Example: Checking PHP and Composer Installation

Open your terminal and run the following commands:

php -v

composer -v

PHP 7.4.3 (cli) (built: Feb 18 2020 12:48:46) ( NTS )
Composer version 1.10.1 2020-03-13 20:34:27

Installing Symfony

Use Composer to create a new Symfony project:

Example: Creating a Symfony Project

Run the following command in your terminal:

composer create-project symfony/skeleton my_project_name

Creating a "symfony/skeleton" project at "./my_project_name"
Installing symfony/skeleton (v5.1.0)
...

Directory Structure

When the installation is complete, your project's directory structure will look like this:

Example: Symfony Project Structure

my_project_name/
├── bin/
├── config/
├── public/
├── src/
├── templates/
├── translations/
├── var/
└── vendor/

Running the Symfony Server

Symfony comes with a local web server that you can use for development. To start the server, run the following command:

Example: Starting Symfony Server

Run the following command in your project directory:

symfony serve

[OK] Web server listening
The Web server is using PHP CGI 7.4.3
http://127.0.0.1:8000

Creating Your First Controller

Controllers are responsible for handling requests and returning responses. To create a controller, use the Symfony console:

Example: Creating a Controller

Run the following command:

php bin/console make:controller DefaultController

created: src/Controller/DefaultController.php
created: templates/default/index.html.twig

Routing

Routing defines how your application responds to different URLs. Symfony uses annotations to define routes in the controller.

Example: Defining a Route

Edit src/Controller/DefaultController.php:

<?php

namespace App\Controller;

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

class DefaultController extends AbstractController
{
    /**
     * @Route("/", name="home")
     */
    public function index(): Response
    {
        return $this->render('default/index.html.twig', [
            'controller_name' => 'DefaultController',
        ]);
    }
}
                    

Creating Views

Views are used to render the HTML content. Symfony uses Twig as its templating engine.

Example: Creating a View

Edit templates/default/index.html.twig:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Welcome!</title>
</head>
<body>
    <h1>Hello {{ controller_name }}!</h1>
</body>
</html>
                    

Database Configuration

Symfony supports various database engines. Configure your database settings in the .env file:

Example: Configuring the Database

Edit the .env file:

DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name"
                    

Creating an Entity

Entities are PHP classes that map to database tables. To create an entity, use the Symfony console:

Example: Creating an Entity

Run the following command:

php bin/console make:entity

Class name of the entity to create or update (e.g. BraveGnome, App\Entity\BraveGnome): Product
New property name (press <return> to stop adding fields): name
Field type (enter ? to see all types) [string]: string
Field length [255]:
...

Running Migrations

After creating an entity, you need to run migrations to create the corresponding database table:

Example: Running Migrations

Run the following commands:

php bin/console make:migration

php bin/console doctrine:migrations:migrate

[OK] Successfully executed migration.

Conclusion

Congratulations! You have successfully created a basic Symfony application. This tutorial covered the essentials to get you started with Symfony. For more advanced features, refer to the official Symfony documentation.