Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Updated: Comment #N
Problem/Motivation
The \Symfony\Component\HttpKernel\EventListener\RouterListener
operates in two modes (according to the doxygen comments of the class):
- 2.3 compatibility mode where you must call setRequest whenever the Request changes.
- 2.4+ mode where you must pass a RequestStack instance in the constructor.
When operating in 2.4+ mode, the router listener updates the request context from within its KernelEvents::REQUEST
event handler. This allows us to decouple the router.request_context
service from the request
service.
Proposed resolution
Decouple router.request_context
from request
service and instead let router_listener
update the request context for us.
Remaining tasks
User interface changes
API changes
Comment | File | Size | Author |
---|---|---|---|
#28 | 2223593-use-request-stack-in-router-28.diff | 1.68 KB | znerol |
Comments
Comment #1
znerol CreditAttribution: znerol commentedComment #3
znerol CreditAttribution: znerol commentedReroll after #1316692: Convert hook_admin_paths() into declarative properties on routes. How do we educate devs to use request stack service instead of request service in new code?
Comment #5
znerol CreditAttribution: znerol commentedSupposedly the test failures are a result of working with the url generator lacking an appropriate request object in the parent site. As a consequence the global
url
function might produce different results for the parent site and the child site.Therefore it may be necessary to setup the request stack in the simpletest parent site, such that a proper
request
is available through therequest_stack
service.Comment #6
znerol CreditAttribution: znerol commentedThe natural place to inject the request-context into the test-container would be either
TestBase::rebuildContainer
orWebTestBase::prepareRequestForGenerator
. However none of them works because further down insetUp
we're callingdrupal_flush_all_caches
and that one ends up inDrupalKernel::boot()
. In factDrupalKernel::updateModules()
reboots the kernel for the reason stated in the following comment:The reason why the
$request
service is not affected, is that the kernel takes care of restoring it when rebuilding the container. We probably need to do the same for request-context.I think that the proper way to resolve the problem would be to delay booting the kernel until after
WebTestBase::resetAll
. However, that does not seem practical given that a lot of services are accessed throughoutWebTestBase::setUp
.Comment #7
znerol CreditAttribution: znerol commentedLet's try that.
Comment #8
znerol CreditAttribution: znerol commented@dawehner questions the hunk in
WebTestBase::prepareRequestForGenerator
. Let's see whether we can live without it.Comment #9
znerol CreditAttribution: znerol commentedGet rid of custom persist logic for the
router.request_context</service>. The custom logic actually is only required for the <code>request
service because it potentially is scoped.Persisting
router.request_context
service actually is necessary because the reasons given in #6: In short we end up with an empty request context and that will affect the URL generator (and thus theurl
function) in the simpletest parent.Comment #10
znerol CreditAttribution: znerol commentedComment #11
znerol CreditAttribution: znerol commentedThe change to
Symfony\Cmf\Component\Routing\ProviderBasedGenerator
was accidental. Lets revert that and at the same time revert the changes to theDrupal\Core\Routing\UrlGenerator
to restore consistency with CMF.Comment #13
znerol CreditAttribution: znerol commentedSilly me, failed to add the '@'-character to one of the container service references.
Comment #14
znerol CreditAttribution: znerol commentedComment #15
znerol CreditAttribution: znerol commentedComment #16
znerol CreditAttribution: znerol commentedReroll, no changes.
Comment #17
znerol CreditAttribution: znerol commentedReroll after #2229223: RequestStack is not persisted across kernel rebuilds
Comment #18
znerol CreditAttribution: znerol commentedComment #19
dawehnerIs there a reason for this change? Does that mean that the router listener ensures that?
Comment #20
znerol CreditAttribution: znerol commentedUpon closer inspection the change to the url generator service definition does not seem to be strictly required as part of this patch. Let's try and revert that change.
Exactly. In 2.3 mode, the request stack is not injected into the router listener. The router listener therefore cannot possibly update the request context by itself. Whereas in 2.4 mode the router listener does maintain the request context from within a
KernelEvents::REQUEST
handler. (see RouterListener::onKernelRequest()). In the simpletest parent site we still need to update the request context manually. Before this was done upon synchronization of the request service - i.e. whenever something called\Drupal::service('request')->set(...)
.Comment #21
znerol CreditAttribution: znerol commentedComment #22
znerol CreditAttribution: znerol commentedUh, no test results, let's trigger a test-run.
Comment #23
dawehner.
Comment #25
znerol CreditAttribution: znerol commented20: 2223593-use-request-stack-in-router-20.diff queued for re-testing.
Comment #26
znerol CreditAttribution: znerol commentedThis looks like a testbot failure. Retest and back to RTBC.
Comment #28
znerol CreditAttribution: znerol commentedReroll.
Comment #29
dawehnerre-rtbc
Comment #30
tim.plunkettHaven't read the issue yet... Just putting it where people can find it.
Comment #31
alexpottCommitted 775184c and pushed to 8.x. Thanks!