Skip to end of metadata
Go to start of metadata

Redirection Notice

Icon
This page will redirect to https://www.jetbrains.com/help/phpstorm/symfony-service-container.html in about 2 seconds.

 

Working with the Service Container

Symfony2 applications are full of objects. Some are inherent to using Symfony2 as the development platform for our application, such as the Session, Doctrine or FileSystem objects. We also have our own objects that are specific to our application. The Symfony service container helps us instantiate, organize and retrieve these objects. Next to that, it also holds a collection of parameters that we can use to configure the behavior or specific values that our application can use.

Using the Symfony2 Plugin, we get completion and navigation on the services and parameters we register with the Symfony2 service container. This reduces the need for PHPDoc type hints in our codebase.

Completion and Navigation for Services

When we want to consume a service from the service container, we can use its name to retrieve it. PhpStorm provides completion (Ctrl+Space) for registered service names.

Once we have an instance of the service, the editor will know its type and provide completion (Ctrl+Space) for the specific service as well. No need to add a PHPDoc type hint to our $fs variable here: PhpStorm knows the type.

We can use navigate to declaration to jump to a service implementation. Simply hover over its name and Ctrl+Click (or CMD+Click on Mac OS X) to navigate, or place the cursor on the service name and press Ctrl+B (or CMD+B on Mac OS X).


From the service implementation, we can go to the place it is registered as well. Using navigate to declaration again will, in this case, open the services.xml file in which the Filesystem class is registered. From here, we can also navigate back to either the registered implementation or the parameter being used.

Icon

Completion and navigation for services is also available in YAML and XML configuration files.

Completion for Parameters

PhpStorm provides completion (Ctrl+Space) for registered parameters when fetching a parameter from the service container. PhpStorm will list all defined parameters known to the application.

Missing Service Inspection

When we try to make use of a service that is not registered with the service container (or make a typo), we will get a warning from the Symfony2 Plugin's "Missing Service" inspection. This way, we know we have an issue in our code that needs fixing.

Duplicate Service Inspection

When we try to make use of a service that is already registered with the service container, we will get a warning from the Symfony2 Plugin's "Duplicate Service" inspection. This way, we know we have an issue in our code that needs fixing.

Registering Services

Services are typically registered with the service container through the use of a YAML or XML configuration file. Here's an example of how a Twig extension can be registered with the service container:

In their smallest form, service registrations consist of a name for the service and the class that implements the service. Often, service registrations consist of additional directives like parameters that should be supplied by the service container when instantiating the service.

The Symfony2 Plugin comes with a "Create Service" tool that can help us write the YAML or XML registration. It can be invoked using the Create Service context menu.

In the window that opens, we have to provide the FQN class name that we want to register with the service container. Then we click the Generate button to populate all potential functions that need to be part of the registration.

For every function in the class, we can have the service container assign parameters on creation. We can pick whether we want to assign to the parameter or not by selecting or deselecting the Act check-box. In the Service column, we can select the service that should be injected into the parameter.

A live preview of the registration is shown at the bottom of the Create Service window. We can have it generate YAML or XML by changing the radio button value. Once we're done, we can Copy to Clipboard and paste the generated service registration in, for example, our services.yml file.

 

  • No labels