Symfony Service tutorial

Symfony service tutorial shows how to create a service in Symfony. The service fetches data from a database.


Symfony is a set of reusable PHP components and a PHP framework for web projects. Symfony was published as free software in 2005. The original author of Symfony is Fabien Potencier. Symfony was heavily inspired by the Spring Framework.

Symfony service

The functionality of a Symfony application is divided into smaller chunks called services. A service is a PHP object. Services live in a Symfony service container. There are many built-in services. Services can be autowired in a Symfony application by using type hints.

A list of available services is generated with the php bin/console debug:container command.

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
    resource: '../src/*'
    exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

This is a piece of a services.yaml configuration file. The PHP classes in the src directory can be automatically injected into our code via type hints.

Symfony service example

In the following example, we fetch data from a MySQL database. The data retrieval is delegated to a specific application component: a Symfony service.

$ composer create-project symfony/skeleton simpleservice

With composer, we create a new Symfony skeleton project.

$ cd simpleservice

We go to the project directory.

$ composer require annotations
$ composer require maker
$ composer require symfony/orm-pack

We install three modules: annotations, maker, orm-pack.

$ composer require server --dev

We install the development web server.

    name VARCHAR(100), population INT);

INSERT INTO countries(name, population) VALUES('China', 1382050000);
INSERT INTO countries(name, population) VALUES('India', 1313210000);
INSERT INTO countries(name, population) VALUES('USA', 324666000);
INSERT INTO countries(name, population) VALUES('Indonesia', 260581000);
INSERT INTO countries(name, population) VALUES('Brazil', 207221000);
INSERT INTO countries(name, population) VALUES('Pakistan', 196626000);
INSERT INTO countries(name, population) VALUES('Nigeria', 186988000);
INSERT INTO countries(name, population) VALUES('Bangladesh', 162099000);
INSERT INTO countries(name, population) VALUES('Nigeria', 186988000);
INSERT INTO countries(name, population) VALUES('Russia', 146838000);
INSERT INTO countries(name, population) VALUES('Japan', 126830000);
INSERT INTO countries(name, population) VALUES('Mexico', 122273000);
INSERT INTO countries(name, population) VALUES('Philippines', 103738000);

This is some test data. It creates a small table in MySQL. We can execute the file with the MySQL source command.


In the .env file, we configure the database URL.

$ bin/console make:controller DataController

A DataController is created with bin/console.


namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Service\DataService;

class DataController extends AbstractController
     * @Route("/data", name="data")
    public function index(DataService $dserv)
        $countries = $dserv->findAll();
        return $this->json([
            'data' => $countries

The DataController returns all rows from the countries table in a JSON format. It uses the DataService service.

public function index(DataService $dserv)

The DataService is created via parameter injection.


namespace App\Service;

use Doctrine\DBAL\Driver\Connection;

class DataService 
    protected $conn;

    public function __construct(Connection $conn) 
        $this->conn = $conn;

     * Finds all countries
    public function findAll() {

        $data = $this->conn->fetchAll("SELECT * FROM countries");

        return $data;

The DataService contains a method that retrieves all rows from the countries table. It uses Symfony's DBAL to execute queries.

public function __construct(Connection $conn) 
    $this->conn = $conn;

The service also uses autowiring to create the Connection object.

public function findAll() {

    $data = $this->conn->fetchAll("SELECT * FROM countries");

    return $data;

The findAll() method retrieves all rows from the table.

$ bin/console server:start

The web server is started.

$ curl localhost:8000/data

We create a request with curl command.

In this tutorial we have created a simple service in Symfony. The service fetches data from the database and is autowired in a Symfony controller.

You might also be interested in the following related tutorials: Introduction to Symfony, Symfony validation tutorial, Symfony DBAL tutorial, Symfony upload file tutorial, Symfony form tutorial, PHP tutorial.