Sling Academy
Home/PHP/Getting Started with Symfony: ‘Hello World’ Example

Getting Started with Symfony: ‘Hello World’ Example

Last updated: January 12, 2024

Introduction

When starting to learn any new framework, it’s always a good practice to begin with a ‘Hello World’ example. This tutorial will guide you through the steps of setting up a basic ‘Hello World’ app using Symfony, one of the leading frameworks for building web applications in PHP.

Prerequisites

  • PHP 7.2.5 or higher installed on your system
  • Composer – PHP’s dependency manager
  • A text editor or IDE of your choice
  • A basic understanding of PHP

Installing Symfony

Symfony can be easily installed on any platform using Composer:

composer create-project symfony/website-skeleton hello_world

This command creates a new directory called ‘hello_world’ with a basic Symfony project within it.

Setting Up the Web Server

Symfony provides a web server bundle that is super handy for development:

composer require symfony/web-server-bundle --dev

To start the server, navigate to your project directory and run:

php bin/console server:run

Your application will be available at ‘http://localhost:8000’.

Creating a Controller

In Symfony, logic is defined within ‘controllers’. Let’s create a controller to handle our ‘Hello World’ output:

php bin/console make:controller HelloWorldController

This will generate a new controller class and a template for us.

Your First Route and Controller

Open the generated controller located at ‘src/Controller/HelloWorldController.php’ and modify the ‘index’ method:

use Symfony\Component\HttpFoundation\Response;

// ...

public function index(): Response
{
    return new Response('<html><body>Hello World!</body></html>');
}

Visit ‘http://localhost:8000/hello-world’ to see the output.

Using Templates

We can simplify our controller’s action by returning a rendered template:

use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

// ...

private $twig;

public function __construct(Environment $twig)
{
    $this->twig = $twig;
}

public function index(): Response
{
    return new Response($this->twig->render('hello_world/index.html.twig'));
}

The template file ‘templates/hello_world/index.html.twig’ may look like this:

<html>
    <head><title>Hello World!</title></head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

Routes Configuration

Let’s define a route in config/routes.yaml:

hello_world:
    path:  /hello-world
    controller: App\Controller\HelloWorldController::index

This tells Symfony that whenever the ‘/hello-world’ path is hit, it should execute the ‘index’ method on our ‘HelloWorldController’.

Database Connectivity

Although not necessary for a ‘Hello World’ app, it’s useful to know how to connect to a database. This can be achieved by updating the ‘.env’ file with your database credentials and running:

php bin/console doctrine:database:create

This creates a new database that can be used in your Symfony project.

Conclusion

This tutorial provided a step-by-step guide on creating a simple ‘Hello World’ application using Symfony. By following this tutorial, you should now have a basic understanding of Symfony components such as routes, controllers, and views. Keep exploring Symfony’s features to build more complex applications.

Next Article: Symfony Best Practices: Directory Structure and Code Organization

Previous Article: What is PHP Symfony and Why Should You Care?

Series: Symfony & Doctrine Tutotirlas

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array