9951 explained code solutions for 126 technologies


php-symfonyHow to create a "Hello World" in PHP Symfony?


Creating a "Hello World" in PHP Symfony is a simple task.

Example code

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class HelloWorldController
{
    public function helloWorld()
    {
        return new Response('Hello World!');
    }
}

Output example

Hello World!

Code explanation

  • namespace App\Controller; - declares the namespace of the controller
  • use Symfony\Component\HttpFoundation\Response; - imports the Response class from the Symfony HttpFoundation component
  • class HelloWorldController - declares the controller class
  • public function helloWorld() - declares the helloWorld method
  • return new Response('Hello World!'); - returns a new Response object with the string "Hello World!"

Helpful links

Edit this code on GitHub