New in Symfony 5.1: Simpler Request Context

Contributed by
Nicolas Grekas
in #36651.

Generating URLs in console commands and any other places outside of the web context is challenging because Symfony doesn't have access to the current host, the base URL, etc.

The solution is to configure the request context using container parameters such as router.request_context.host. In Symfony 5.1 we've already improved this allowing you to configure these values via the framework.router option. However, we kept working on this feature to simplify it even more.

That's why in Symfony 5.1, you'll be able to configure the entire request context using a single default_uri parameter, instead of having to define several parameters:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Before
framework:
    router:
        host: 'example.org'
        scheme: 'https'
        base_url: 'my/path'

# After
framework:
    router:
        default_uri: 'https://example.org/my/path/'

Related to this, in the pull request #36681, we've updated the assets config to reuse the router context by default. This means that most of the times, defining the default_uri option is enough to configure both the request context and the assets context.

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Comments

most of the time? Is it inconsistent?
@Craig this feature is consistent ... but the application may not :) Symfony applies the same context to request and assets ... but your app may need a different context for assets. That's why most of the times it's enough to define just the request context ... but some apps might need to still define both the request and asset contexts.
Will this be applied to the asset urls generated in a command (i.e. where there is not request) ?
I was wondering if all all these little changes get documented?
@Michaël yes, we'll document this change as we do with all the other changes. Sometimes it takes a bit of time, but we ultimately document everything.

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.