In addition to letting routes choose their firewall, Symfony 8.2 improves the Routing component with more features.

Default Query Parameters

Ousama Ben Younes
Contributed by Ousama Ben Younes in #65073

Symfony 7.3 introduced the special _query parameter to add query parameters when generating URLs. However, routes couldn't define them, so you had to repeat them in every generate() and path() call.

In Symfony 8.2, routes can define a _query default with those parameters. It's a regular route default, so it works in all configuration formats:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// src/Controller/ProductController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\Routing\Attribute\Route;

class ProductController
{
    #[Route('/products', name: 'product_list', defaults: ['_query' => ['page' => 1, 'sort' => 'name']])]
    public function list(#[MapQueryParameter] int $page, #[MapQueryParameter] string $sort): Response
    {
        // for the /products URL, $page is 1 and $sort is 'name'
        // for the /products?page=2 URL, $page is 2 and $sort is 'name'
    }
}

These defaults don't affect route matching, which never uses the query string. As the example shows, Symfony also adds them to $request->query when the request doesn't include them. When generating URLs, the values that you pass win over the defaults:

1
2
3
4
5
6
7
8
9
$urlGenerator->generate('product_list');
// /products?page=1&sort=name

$urlGenerator->generate('product_list', ['page' => 2]);
// /products?page=2&sort=name

// use a null value to remove a default query parameter
$urlGenerator->generate('product_list', ['_query' => ['sort' => null]]);
// /products?page=1

Combining Route Conditions

Nicolas Grekas
Contributed by Nicolas Grekas in #66004

When importing routes, the condition option replaces the condition of each imported route, so a condition for the whole group can't coexist with one defined in #[Route(condition: '...')].

Symfony 8.2 adds the add_condition option (addCondition() in PHP), which combines the given expression with the existing condition of each route:

1
2
3
4
5
# config/routes.yaml
api:
    resource: ../src/Controller/Api/
    type: attribute
    add_condition: "request.headers.get('X-Api-Version') == '2'"

Conditions are combined with and. If one of those controllers defines #[Route(condition: "context.getMethod() in ['GET', 'HEAD']")], the condition of that route is now:

1
(context.getMethod() in ['GET', 'HEAD']) and (request.headers.get('X-Api-Version') == '2')

Generating URLs for Another Context

Hubert Lenoir
Contributed by Hubert Lenoir in #64603

URLs are generated using the host, scheme and base URL of the current request. Multi-tenant and multi-domain applications often need a different one, e.g. to include links to the domain of each tenant in an email. Until now, you had to change the shared RequestContext and restore every value yourself, which is easy to get wrong.

Symfony 8.2 adds a RequestContext::runWith() method that applies the given values while it runs a callback and restores the original ones afterwards, even if the callback throws an exception:

1
2
3
4
5
6
7
8
9
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

$invoiceUrl = $urlGenerator->getContext()->runWith(
    fn () => $urlGenerator->generate('invoice_show', [
        'id' => $invoice->getId(),
    ], UrlGeneratorInterface::ABSOLUTE_URL),
    host: $tenant->getDomain(),
    scheme: 'https',
);
// https://acme.example.com/invoices/1234

The method also accepts the baseUrl, method, httpPort, httpsPort, pathInfo, queryString and parameters arguments. The ones that you don't pass keep their current values.

The values are applied to the context object itself, so all the services that share it use them inside the callback: the router, the UrlHelper, the Twig url() and path() functions, etc.

Published in #Living on the edge