What Is New in Symfony 7.1
Symfony 7.1 delivers a focused set of enhancements and new features, primarily improving developer experience and component integrations. This release continues the framework's evolution with practical additions to the Form, Mailer, and DependencyInjection components, alongside several quality-of-life improvements.
| Category | Key Changes |
|---|---|
| New Features | Form theme for WebComponents, Mailer ping command, service autowiring for typed collections. |
| Improvements | Better HttpClient retry strategies, enhanced ClockMock, new Number constraint options. |
| Deprecations | Legacy session service aliases, some legacy form type options. |
| Bug Fixes | Various fixes across the Console, Form, and HttpKernel components. |
How does Symfony 7.1 improve form handling for modern frontends?
The Form component now includes a built-in form theme for Web Components. This allows you to seamlessly integrate Symfony forms with custom HTML elements that use the Shadow DOM. In practice, it means your form rendering can directly target elements like <my-input>.
You enable it by configuring the form theme in your Twig configuration. This matters because it bridges the gap between Symfony's robust server-side form generation and modern, component-based frontend architectures without requiring complex custom workarounds.
twig:
form_themes:
- 'form_webcomponents_layout.html.twig'
What new tools are available for testing and debugging?
Symfony 7.1 adds a mailer:ping command to test your Mailer transport connection. Just run php bin/console mailer:ping to check if your configured mailer DSN is working. This is a quick way to verify credentials and network connectivity during deployment or troubleshooting.
The ClockMock class from the PHPUnit bridge has been enhanced to also mock hrtime() and microtime(). This ensures all time-related functions behave consistently in your tests, which is crucial for reliable and deterministic test suites, especially for timing-sensitive logic.
How does autowiring get more powerful in this release?
Dependency Injection now supports autowiring of typed collections. You can now type-hint an argument with array or iterable and use the #[AutowireIterator] or #[AutowireLocator] attribute to inject specific sets of tagged services.
This simplifies a common pattern where you needed to manually configure service locators or iterator arguments in YAML or XML. Now it's cleaner and lives directly in your service class code.
class MyService
{
public function __construct(
#[AutowireIterator('app.handler')]
iterable $handlers
) {}
}
Are there improvements for building resilient HTTP clients?
Yes, the HttpClient component introduces more flexible retry strategies. You can now use a RetryableHttpClient with a jitter parameter, which adds randomness to retry delays. This helps prevent the "thundering herd" problem where many failed requests simultaneously retry at the same time.
The GenericRetryStrategy also allows you to define custom retry status codes and HTTP methods. This gives you fine-grained control over which requests are worth retrying based on your specific API's failure modes.
What validation constraints have been updated?
The Number constraint received new options: divisibleBy and identicallyEqualTo. The divisibleBy option validates that a number is divisible by another given number, while identicallyEqualTo enforces type-strict equality (e.g., 5 is not identical to "5").
These additions provide more precise validation rules out of the box, reducing the need for custom callback constraints for common numerical validation scenarios.
FAQ
How do I start using the new Web Components form theme?
Configure it as a global form theme in your Twig configuration file. Then, ensure your Web Component custom elements use the expected data attributes for things like errors and labels, similar to standard HTML form elements.
Is the new `mailer:ping` command suitable for production health checks?
It can be, but use it cautiously. The command tests the connection at the moment it's run, which could expose credentials in process listings or logs. For automated health checks, a custom endpoint that uses the mailer transport indirectly might be safer.
What happens if I use the deprecated session service aliases?
You'll get a deprecation notice. Update your code to inject the main RequestStack service or the session service from the container instead of the old aliases like session.flash_bag.
When should I use `#[AutowireIterator]` versus `#[AutowireLocator]`?
Use #[AutowireIterator] when you just need to loop over all services of a certain tag. Use #[AutowireLocator] when you need to fetch a specific service from the collection by a key (like a named handler).
Does the `ClockMock` improvement affect the `time()` function?
Yes. The ClockMock was already mocking time(), sleep(), and usleep(). Symfony 7.1 extends this to also mock hrtime() and microtime(), covering more time functions used in performance measurement and precise timing.