You are currently viewing Hello World in Symfony

Hello World in Symfony

Symfony is a popular PHP framework that follows the Model-View-Controller (MVC) architectural pattern. It provides a robust set of tools and libraries for developing web applications rapidly. In this tutorial, we’ll create a simple “Hello World” application in Symfony to get you started.

Prerequisites

  • PHP installed on your machine (PHP 7.4 or higher recommended)
  • Composer installed (Dependency Manager for PHP)
  • Symfony CLI installed (optional but recommended)

Table of Contents

  1. Install Symfony
  2. Create a New Symfony Project
  3. Create a Controller
  4. Create a Route
  5. Create a Template
  6. Run the Application
  7. Conclusion

1. Install Symfony

If you haven’t installed Symfony CLI yet, you can do so using Composer:

composer global require symfony/cli

Make sure to add Composer’s system-wide vendor bin directory to your $PATH so the Symfony CLI executable can be located.

2. Create a New Symfony Project

Using Symfony CLI, you can create a new Symfony project easily:

symfony new my_symfony_app --full

This command will create a new Symfony application with all the necessary components included.

3. Create a Controller

In Symfony, controllers are responsible for handling user requests and returning responses. Let’s create a simple controller:

cd my_symfony_app
symfony console make:controller HelloWorldController

Follow the prompts to create the controller. This will generate a new controller class in src/Controller/HelloWorldController.php.

Open src/Controller/HelloWorldController.php and add a simple action method:

// src/Controller/HelloWorldController.php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class HelloWorldController extends AbstractController
{
    /**
     * @Route("/hello-world", name="hello_world")
     */
    public function index(): Response
    {
        return $this->render('hello_world/index.html.twig', [
            'message' => 'Hello, Symfony!',
        ]);
    }
}

4. Create a Route

Routes in Symfony map URLs to controller actions. Let’s create a route for our HelloWorldController:

Open config/routes.yaml and add the following route:

# config/routes.yaml
hello_world:
    path: /hello-world
    controller: App\Controller\HelloWorldController::index

5. Create a Template

Symfony uses Twig as its templating engine. Let’s create a simple template for our “Hello World” message:

Create a new file templates/hello_world/index.html.twig:

{# templates/hello_world/index.html.twig #}

<!DOCTYPE html>
<html>
<head>
    <title>Hello Symfony</title>
</head>
<body>
    <h1>{{ message }}</h1>
</body>
</html>

6. Run the Application

Now you’re ready to run your Symfony application:

symfony server:start

Open your browser and visit http://localhost:8000/hello-world. You should see the “Hello, Symfony!” message displayed.

7. Conclusion

Congratulations! You’ve created a simple “Hello World” application in Symfony. You learned how to create a controller, define a route, and create a basic Twig template. Symfony provides powerful features for building web applications, and this is just the beginning. Explore Symfony’s documentation and tutorials to discover more about what you can do with this powerful framework.

Additional Resources:

Leave a Reply