-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
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