Skip to content

Route parameter mapping doesn't work with forwarded requests #61185

@thomas-hiron

Description

@thomas-hiron

Symfony version(s) affected

7.3.1

Description

When forwarding a request to another controller, route parameter mapping doesn't work due to the RouterListener.

In fact, the RouterListener has an early return if the _controller attribute exists.
This attribute is set by the forward helper:

protected function forward(string $controller, array $path = [], array $query = []): Response
{
    $request = $this->container->get('request_stack')->getCurrentRequest();
    $path['_controller'] = $controller;
    $subRequest = $request->duplicate($query, null, $path);

    return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}

The _route_mapping attribute is not set correctly and the following error occurs:

Controller "App\Controller\AppController::anotherEntity" requires the "$anotherEntity" argument that could not be resolved. Possible reasons: 1) Cannot find mapping for "App\Entity\AnotherEntity": declare one using either the #[MapEntity] attribute or mapped route parameters. 2) Cannot autowire argument $anotherEntity of "App\Controller\AppController::anotherEntity()": it needs an instance of "App\Entity\AnotherEntity" but this type has been excluded in "config/services.yaml".

It works well with the #[MapEntity] attribute, but we'd prefer to use route parameters!

How to reproduce

#[Route(path: '/{uuid:entity}', name: 'entity', requirements: ['uuid' => '%app.pattern_uuid%'], methods: 'GET')]
public function entity(Entity $entity): Response
{
    return $this->forward('App\Controller\EntityController::anotherEntity', [
        'uuid' => $entity->getAnotherEntity()->getUuid(),
    ]);
}

#[Route(path: '/another-entity/{uuid:anotherEntity}', name: 'another_entity', requirements: ['uuid' => '%app.pattern_uuid%'], methods: 'GET')]
public function anotherEntity(AnotherEntity $anotherEntity): Response
{
    return new Response('should work');
}

Possible Solution

Should the forward helper call the RequestMatcherInterface::matchRequest?

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy