Symfony applications comprise multiple objects called services. Some services, such as
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.
Install the required plugins
The plugins are available only in IntelliJ IDEA Ultimate.
Press Ctrl+Alt+S to open the IDE settings and select.
Switch to the Marketplace tab and use the search field to find the plugins.
Click Install next to each plugin and restart the IDE if prompted.
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.
Complete methods' names for the instantiated service.
Fetch registered parameters fetched from the service container.
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 the usage.
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.
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, IntelliJ IDEA displays the corresponding warning from the Missing Service inspection. .
When you register a service that is already registered with the service container, IntelliJ IDEA displays the corresponding warning from the Duplicate Service inspection. .
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:
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
Right-click in the code editor and select Create Service from the context menu.
In the Service Generator dialog that opens, provide the FQN of the class to register with the service container in the Class field.
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.
Choose whether to generate YAML or XML by choosing the corresponding option.
Click Copy to Clipboard and paste the generated service registration into the configuration file, for example, services.yml.