PhpStorm 2019.1 Help

Service Container

Symfony applications comprise multiple objects called services. Some services, such as Session, Doctrine, or FileSystem, are inherent to using Symfony as the development platform. Other objects are specific to the application being developed. The Symfony service container helps instantiate, organize and retrieve these objects. It also holds a collection of parameters letting configure the behavior or specific values that the application can use.

The Symfony Plugin provides code completion and navigation for the services and parameters that are registered with the Symfony service container and assist you in registering new ones.

Code completion for Symfony services

In the editor, press Ctrl+Space to invoke code completion and do any of the following:

  • Retrieve a registered service from the service container.

    Symfony service name completion
  • Complete methods' names for the instantiated service.

    Symfony service methods completion
  • Fetch registered parameters fetched from the service container.

    Symfony service parameters completion

Code navigation for services

To navigate to the declaration of a service, position the caret at its usage and press Ctrl+B. Alternatively, Ctrl+Click (for Windows and Linux) or ⌘+Click (for macOS) the usage.

Navigating to Symfony service name declaration

The Navigate to declaration command will also let you navigate between the service implementation and the route it is registered in. For example, you can jump from the Filesystem class to the services.xml file in which the class is registered. From there, you can navigate back to either the registered implementation of the service or its usage.

Navigating between Symfony service implementation and declaration

Code inspection for services

The Symfony Plugin provides the several inspections for detecting errors in the services registration.

  • When you use a service that is not registered with the service container, PhpStorm displays the corresponding warning from the Missing Service inspection..

    Symfony missing service inspection
  • When you register a service that is already registered with the service container, PhpStorm displays the corresponding warning from the Duplicate Service inspection..

    Symfony duplicate service inspection

Registering services

Services are typically registered with the service container by adding the corresponding records in a YAML or an XML configuration file. For example, to register a Twig extension with the service container, you can add the following record:

services: blogger_blog.twig.extension: class: Blogger\BlogBundle\DependencyInjection\BloggerBlogExtension tags: - { name: twig.extension }

A service registration record, at minimum, consists of a name for the service and the class that implements it. Additionally, it commonly contains the directives for the parameters that should be supplied by the service container when instantiating the service.

The Symfony Plugin provides the Create Service tool that can assists in creating the YAML or XML registration.

Register a service

  1. Right-click in the code editor and select Create Service from the context menu.

    Symfony create service command
  2. In the Service Generator dialog that opens, provide the FQN of the class to register with the service container in the Class field.

    the Symfony Service Generator dialog

    The list of potential methods that need to be part of the registration populates automatically. If necessary, click Generate to refresh it.

    • On creating the service, you can have the service container assign parameters for every function in the class. This is regulated by selecting or deselecting the Act checkbox.

    • In the Service column, you can select the service that should be injected into the parameter.

  3. Choose whether to generate YAML or XML by choosing the corresponding option.

  4. Click Copy to Clipboard and paste the generated service registration into the configuration file, for example, services.yml.

Last modified: 26 July 2019