Learn Symfony Β· 1/12

Introduction and Environment Setup

6 min read

Welcome to the Symfony track. If PHP is the engine of the web, Symfony is the luxury chassis, the precision steering, and the climate control system wrapped into one. 🏎️

In this guide, we are moving away from "spaghetti code" (where logic, database queries, and HTML are mixed in one file) and stepping into the world of Frameworks.


1. The 'Why' Before the 'How': Why Choose Symfony?

Before we type a single line of code, we need to address the elephant in the room: Why do we need a framework?

The Problem: The "Include" Nightmare

In raw PHP, as your application grows, you end up with hundreds of files including each other. You have to manually sanitise every user input to prevent hacking (SQL Injection). You have to write your own router (the logic that decides which page to show based on the URL).

Doing this manually is like building your own car every time you want to drive to the grocery store. It’s impressive, but you’ll starve before you get there.

The Solution: Symfony

Symfony is a set of reusable PHP components and a PHP framework.

  1. Strict Standards: Symfony forces you to write clean, readable code. It adheres strictly to PSR (PHP Standards Recommendations).
  2. The Component System: This is the industry secret. Symfony isn't just one giant block of code. It is made of small, standalone libraries (Components).
    • Fact: Did you know that Laravel, Drupal, and Magento are built on top of Symfony components? Even if you aren't using Symfony the Framework, you are likely using Symfony the Code.
  3. Stability: Symfony has a predictable release cycle and Long Term Support (LTS) versions. This is why Enterprises love it. It doesn't break your app every Tuesday.

Industry Insight: In a professional setting, we choose Symfony when we need a project to live for 5-10 years. It prioritizes maintainability over "quick and dirty" speed.


2. Setting Up Your Local PHP and Composer Environment

You cannot build a house without a foundation. For Symfony, that foundation is PHP and Composer.

Step A: PHP ( The Language)

Symfony 7 (the current major version) requires PHP 8.2 or higher.

Under the hood:
Symfony makes heavy use of modern PHP features like Attributes (annotations in code), Union Types, and Constructor Promotion. Old PHP versions simply cannot read Symfony code.

  • Windows: Use Laragon or XAMPP, but ensure the PHP version is 8.2+.
  • Mac: Use brew install php.
  • Linux: Use your package manager (e.g., apt install php8.2).

Verify your installation in your terminal:

bash
php -v

If you see anything lower than 8.2, you must upgrade.

Step B: Composer (The Dependency Manager)

Definition: Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

The Problem it Solves:
Imagine you need a library to generate PDFs. That library needs a font library. That font library needs a math library. Downloading these manually and ensuring they are compatible is "Dependency Hell."

The Solution:
You tell Composer: "I want the PDF library." Composer automatically downloads the PDF library, the font library, and the math library, ensuring all versions match.

Installation:
Follow the official download instructions (link at the bottom). Once installed, check it:

bash
composer -V

3. Creating Your First Symfony Project with Symfony CLI

While you can use Composer to create a project, the industry standard for development is the Symfony CLI.

Why use the CLI tool?

  1. Local Web Server: It comes with a built-in server optimized for Symfony. No need to configure Apache or Nginx configuration files just to start coding.
  2. TLS Certificates: It can generate valid HTTPS certificates for localhost.
  3. Productivity: It includes commands to check security vulnerabilities.

Step A: Install Symfony CLI

Windows:

powershell
scoop install symfony-cli

(Or download the binary from the website if you don't use Scoop).

macOS / Linux:

bash
curl -sS https://get.symfony.com/cli/installer | bash

Step B: Initialize the Project

Navigate to where you keep your code (e.g., cd Projects) and run:

bash
symfony new my_project_name --webapp

Wait, what is --webapp?
Symfony is minimal by default.

  • Without --webapp: You get a micro-framework (Skeleton). Good for simple APIs.
  • With --webapp: You get the "traditional" stack: Template engine (Twig), Database ORM (Doctrine), Logger, etc. We will use this for learning.

Step C: Start the Engines

Move into your folder and start the server:

bash
cd my_project_name
symfony server:start

You should see an output like:

[OK] Web server listening on https://127.0.0.1:8000

Open that link in your browser. If you see the "Welcome to Symfony" page, congratulations! You have successfully installed one of the most powerful frameworks in the world. πŸŽ‰

Funny Imagination: Imagine the Symfony Welcome page as the reception desk of a 5-star hotel. It looks nice, but you aren't allowed to sleep in the lobby. We need to build the rooms.


4. Directory Structure: Finding Your Way Around

When you open your new project in VS Code, the file tree might look intimidating. Let's break down the Directory Structure. We will ignore the clutter and focus on what you actually use daily.

πŸ“‚ public/

This is the Entry Point.

  • Concept: The "Front Controller" pattern.
  • Under the hood: Every single request to your application (whether it's the home page, the login page, or an API call) hits public/index.php first. This file boots up the Symfony "Kernel" (the brain).
  • Practice: You almost never edit files here. This is just where your images and CSS assets live.

πŸ“‚ config/

This is the Control Room.

  • Here you configure how your database connects (doctrine.yaml), how security works (security.yaml), and which routes exist (routes.yaml).
  • In Symfony, we prefer Convention over Configuration, but when we need configuration, it lives here.

πŸ“‚ src/

This is Your Code. 99% of your work happens here.

  • Controller: The logic that handles requests. (e.g., ProductController.php decides what happens when someone visits /products).
  • Entity: Your database structure represented as PHP classes.
  • Repository: The logic to fetch data from the database.

πŸ“‚ templates/

This is the View.

  • Contains .html.twig files. Symfony uses a templating engine called Twig. It separates HTML from PHP logic.

πŸ“‚ vendor/

DO NOT TOUCH. β›”

  • This is where Composer downloads all those libraries we talked about.
  • Fact: If you edit code inside vendor/, the next time you run composer update, your changes will be deleted. It is the "black hole" of third-party code.

πŸ“„ .env

This is Environment Variables.

  • Sensitive data lives here (Database passwords, API keys).
  • Practice: never commit .env.local to GitHub.

Summary Checklist

  1. βœ… PHP 8.2+ and Composer are installed.
  2. βœ… Symfony CLI is ready.
  3. βœ… Project created using --webapp flag.
  4. βœ… Server is running on port 8000.
  5. βœ… You know that src/ is for work, and vendor/ is for the robots.

πŸ”— References & Further Reading

Challenge

Challenge: Symfony Project Initialization and Structure Analysis

Your task is to complete the pseudo-script below. Fill in the placeholders (/* -- YOUR CODE HERE -- */) with the correct Symfony CLI commands and the descriptions for the specified directories and files. This will test your understanding of creating a new Symfony project and its fundamental structure.

Expected result

Your completed script should accurately reflect the standard Symfony workflow and directory structure conventions.

shell
## Pseudo-script for Symfony Project Setup

# Step 1: Use the industry-standard tool to create a new, full-featured Symfony web project named 'my_app'.
symfony new my_app --webapp

# Step 2: After the project is created, what command starts the local development server?
symfony server:start

## Directory and File Purpose Identification

# Step 3: Describe the primary purpose of the 'src/' directory.
# src/: This is the main directory for your application's PHP code, including Controllers, Entities, Repositories, and business logic.

# Step 4: What is the purpose of the 'config/' directory?
# config/: Contains configuration files (usually in YAML format) for routes, services, security, and packages.

# Step 5: What is stored in the 'templates/' directory?
# templates/: Stores all Twig template files (.html.twig) used for rendering the application's frontend.

# Step 6: What is the significance of the '.env' file, and what is the rule regarding version control for it?
# .env: Stores environment-specific variables like database credentials and API keys. It should never be committed to version control.

# Step 7: What manages the 'vendor/' directory, and why should you never edit its contents directly?
# vendor/: This directory is managed by Composer and contains all the third-party libraries (dependencies) for the project. You should not edit it because any changes will be overwritten during a 'composer install' or 'composer update'.
shell

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.