PhpStorm
 
Get PhpStorm

Creating helper functions

Last modified: 17 June 2024

The Symfony Support plugin provides parameter types and return types inference based on their usage with Symfony classes and functions. This lets you create your own helper functions that take or return a template, a service, an entity, a translation, a route, a form, or an event.

As an example, let's create a redirectToRoute function that can be used in controllers. This function will combine the Controller class' redirect and generateUrl function, so that you can easily redirect users to a known route within our application. The code will look as follows:

When we call this function, we will get code completion CtrlSpace for the $route and , as well as the ability to navigate to the corresponding declaration Ctrl0B.

Symfony route name completion in a helper function

Code completion and navigation for the returned $response instance are available, too.

Symfony response name completion in a helper function

The reason both of these work is that the Symfony Support plugin infers the $route's type by analyzing calls and return types for known functions. This type analysis works for most Symfony components (template, service, entity, translation, route, form, or event), but there are cases where it will not work as-is.

Whenever completion and navigation for helper functions that we create ourselves or utilize from a third-party library do not seem to work, the Symfony Support plugin comes with a solution: it provides access to some of its internals through the Method Parameter and Signature Types project settings.