~ read.

Creating services in Symfony3[EN]

Today we will see how to create services in Symfony3 to better encapsulate our code and separate functionalities, this can be us much used in the development of services rest or backend applications, etc. as we see here :
+ Step 1 We create a directory within our bundle called Services.
+ Step 2 Create a file for the class, in my case I will create one that is called Helpers.
+ Step 3 We create the class, with the methods and functionality that we need, in my case to do the test, I will simply have a method.

<?php
namespace AppBundle\Services;
 
class Helpers {
 
    public function hola(){
        return "Hola desde el servicio";
    }
     
}

Step 4. Configure the service in the app / config / services.yml file

services:
 
    app.helpers:
        class: AppBundle\Services\Helpers
        arguments: ["null"]

We are indicating the name or identifier of the service, the class to load and the arguments or other services that will receive this service. Now in one of our actions of any project controller we can call the service.

$helpers = $this->get("app.helpers");
echo $helpers->hola();