1

Is it possible to pass constructor parameters when calling handle in another controller to forward request? Or is there a different way? Currently I'm getting error below. I don't see any example in documentation.

Catchable Fatal Error: Argument 1 passed to My\Bundle\Order\ApiBundle\Controller\ReceiverController::__construct() must implement interface Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface, none given 

service.xml

<services> <service id="my_api.controller.abstract" abstract="true"> <argument type="service" id="security.token_storage"/> </service> <service id="my_api.controller.receiver" class="My\Bundle\Order\ApiBundle\Controller\ReceiverController" parent="my_api.controller.abstract"> <argument type="service" id="my_api.service.api"/> </service> <service id="my_api.controller.sender" class="My\Bundle\Order\ApiBundle\Controller\SenderController" parent="my_api.controller.abstract"> <argument type="service" id="http_kernel"/> </service> </services> 

ReceiverController

/** * @Route("/receiver", service="my_api.controller.receiver") */ class ReceiverController extends AbstractController { private $apiService; public function __construct( TokenStorageInterface $tokenStorage, AaServiceInterface $aService ) { parent::__construct($tokenStorage); $this->apiService = $apiService; } /** * @param Request $request * * @Secure(roles="ROLE_ADMIN") * * @Route("/order/{id}", requirements={"id"="([0-9]){8}"}) * * @return Response */ public function givItToMeAction(Request $request) { $result = $this->apiService->call( $this->getUser(), $request->getPathInfo(), $request->getMethod(), $request->query->all(), $request->getContent() ); return new Response($result); } } 

SenderController

/** * @Route("/sender", service="my_api.controller.sender") */ class ReceiverController extends AbstractController { private $httpKernel; public function __construct( TokenStorageInterface $tokenStorage, HttpKernelInterface $httpKernel ) { parent::__construct($tokenStorage); $this->httpKernel = $httpKernel; } /** * @param Request $request * * @Secure(roles="ROLE_ADMIN") * * @Route("/order/{id}", requirements={"id"="([0-9]){8}"}) * * @return Response */ public function takeItAction(Request $request) { $attributes = [ '_controller' => 'MyOrderApiBundle:Receiver:givItToMeAction', 'request' => $request ]; $subRequest = $request->duplicate($request->query->all(), null, $attributes); return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST); } } 

1 Answer 1

3

As your controller is registered as a service you must use the same notation to refer to the controller that you use when configuring your routing. This means that instead of MyOrderApiBundle:Receiver:givItToMeAction you should use my_api.controller.receiver:givItToMeAction.

Sign up to request clarification or add additional context in comments.

3 Comments

Error changed so it says we're on right track. New error. {"error":{"code":0,"message":"Controller \"my_api.controller.receiver:passIpaDomainRequest\" for URI \"\/sender\/order\/123456\" is not callable."}}
If you are using the controller as a service then you need to use the actual method name with Action on the end.
You beat me to it. I've just sorted it like that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.