Getting Started with the Symfony Framework

Overview

Symfony stands as a robust PHP-based web application framework designed to streamline the development process. Built upon the MVC (Model-View-Controller) architectural pattern, it delivers a collection of reusable PHP components and libraries that enable developers to construct solid, maintainable web applications efficiently.

Installing Symfony

Before diving into Symfony, verify that your system meets these prerequisites:

  • PHP version 7.2.5 or newer
  • Composer (PHP dependency manager)

Create a fresh Symfony project using Composer:

composer create-project symfony/website-skeleton my_app

This command generates a new directory called my_app containing the fundamental Symfony framework structure and dependencies.

Project Structure

A typical Symfony project follows this directory organization:

  • config/: Houses application configuration files
  • public/: Web server document root containing frontend assets
  • src/: Contains the project's PHP source code
  • templates/: Stores Twig template files
  • var/: Contains generated files like cache and logs
  • vendor/: Holds project dependencies

Controlllers

Controllers in Symfony handle HTTP requests and generate HTTP responses. These are PHP classes typically stored in the src/Controller directory.

Here's a basic controller implementation:

// src/Controller/DashboardController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DashboardController
{
    /**
     * @Route("/dashboard", name="dashboard_page")
     */
    public function showDashboard(): Response
    {
        return new Response('Hello from Symfony Dashboard!');
    }
}

In this example, the DashboardController class defines a showDashboard() method linked to the URL path /dashboard. Accessing this URL triggers the method execution, returning a response with "Hello from Symfony Dashboard!" text.

Routing System

Routing in Symfony maps URLs to controller actions. Multiple configuration methods exist, including annotations, YAML, or XML formats.

The previous controller example utilized annotations for route definition:

/**
 * @Route("/dashboard", name="dashboard_page")
 */

This annotation connects the /dashboard URL path to the showDashboard() method in DashboardController, assigning the route name dashboard_page.

Twig Templating

Symfony employs Twig as its default templating engine. Twig offers a clean, readable syntax for template creation, simplifying frontend development tasks.

Consider this basic Twig template:

{# templates/dashboard/main.html.twig #}

<!DOCTYPE html>
<html>
<head>
    <title>Symfony Dashboard</title>
</head>
<body>
    <h1>{{ welcome_text }}</h1>
</body>
</html>

Within the controller, render this template with data:

// src/Controller/DashboardController.php
// ...

public function showDashboard(): Response
{
    return $this->render('dashboard/main.html.twig', [
        'welcome_text' => 'Hello from Symfony Dashboard!',
    ]);
}

The render() method takes two arguments: the template file path and an associative array of template variables. Twig substitutes the welcome_text variable with the passed value.

Tags: Symfony PHP web development Framework mvc

Posted on Wed, 01 Jul 2026 17:25:56 +0000 by skurai