9951 explained code solutions for 126 technologies


php-symfonyHow to create a backend with PHP Symfony?


Creating a backend with PHP Symfony is easy and straightforward.

Install the Symfony framework:

$ composer create-project symfony/website-skeleton my-project

Create a controller:

<?php

namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;

class DefaultController
{
    public function index()
    {
        return new Response('Hello World!');
    }
}

Create a route:

# config/routes.yaml

index:
    path: /
    controller: App\Controller\DefaultController::index

Start the server:

$ cd my-project
$ symfony server:start

Visit the page:

http://localhost:8000

You should now see the "Hello World!" message.

Helpful links

Edit this code on GitHub