PHP / PHP Frameworks and CMS

Exploring Symfony Basics

This tutorial will guide you through the basics of Symfony, a PHP framework for web projects. You will learn how to install Symfony and build a simple web application.

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Introduces popular PHP frameworks and content management systems.

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!".

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help