Exploring Symfony Basics

Tutorial 4 of 5

1. Introduction

This tutorial aims to introduce you to the world of Symfony, a popular PHP framework used for building web applications. Through this tutorial, you will learn how to install Symfony and create a basic web application using it.

By the end of this tutorial, you should be able to:

  • Install and set up Symfony on your local machine
  • Understand the basic structure of a Symfony application
  • Build a simple web application with Symfony

Prerequisites:

  • Basic knowledge of PHP
  • PHP installed on your computer
  • Composer (A Dependency Manager for PHP)

2. Step-by-Step Guide

Installing Symfony

Before you can start working with Symfony, you need to install it on your local machine. The recommended way to install Symfony is to use Composer. Run the following command in your terminal:

composer create-project symfony/website-skeleton myproject

Understanding Symfony's Structure

A Symfony project includes the following key directories:

  • bin/: This directory contains executable files.
  • config/: This directory contains all your application's configuration files.
  • src/: This directory contains all your PHP code.
  • templates/: This directory contains all your Twig templates.
  • public/: This directory contains all the front-end files (CSS, JS, Images, etc.).

3. Code Examples

Creating a Simple Controller

In Symfony, a controller is a PHP function that takes a request and returns a response. Here's a simple example:

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

use Symfony\Component\HttpFoundation\Response;

class HelloController
{
    public function greeting()
    {
        return new Response(
            '<html><body>Hello!</body></html>'
        );
    }
}

In this example, we've created a controller class HelloController with a method greeting(). This method returns a Response object with the HTML content to display.

Creating a Route

To access your controller, you need to create a route. Here's how you can do it:

// config/routes.yaml

hello:
    path: /hello
    controller: App\Controller\HelloController::greeting

In this example, we've defined a route named hello that matches the URL path /hello and maps it to the greeting method in the HelloController class.

4. Summary

In this tutorial, we've learned how to install Symfony and understand its basic structure. We also created a simple controller and defined a route to access it.

For further learning, you can explore the following resources:

5. Practice Exercises

Exercise 1

Create a new route /goodbye and a new controller method goodbye() that returns a response saying "Goodbye!".

Solution

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

use Symfony\Component\HttpFoundation\Response;

class GoodbyeController
{
    public function goodbye()
    {
        return new Response(
            '<html><body>Goodbye!</body></html>'
        );
    }
}
// config/routes.yaml

goodbye:
    path: /goodbye
    controller: App\Controller\GoodbyeController::goodbye

Test your code by visiting http://localhost:8000/goodbye in your web browser.

Exercise 2

Create a new route /greet/{name} and a new controller method greet($name) that returns a response saying "Hello, {name}!" where {name} is a URL parameter.

Solution

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

use Symfony\Component\HttpFoundation\Response;

class GreetController
{
    public function greet($name)
    {
        return new Response(
            '<html><body>Hello, '.$name.'!</body></html>'
        );
    }
}
// config/routes.yaml

greet:
    path: /greet/{name}
    controller: App\Controller\GreetController::greet

Test your code by visiting http://localhost:8000/greet/John in your web browser. It should display "Hello, John!".