Symfony: Forwarding request to another controller with constructor parameters

Created on 14 Jan 2016  路  1Comment  路  Source: symfony/symfony

Hi,

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.

Thanks

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);
    }
}

Most helpful comment

Sorted with: '_controller' => 'my_api.controller.receiver:givItToMeAction',

>All comments

Sorted with: '_controller' => 'my_api.controller.receiver:givItToMeAction',

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sunviwo picture sunviwo  路  3Comments

slashsbin picture slashsbin  路  3Comments

jpresley23 picture jpresley23  路  3Comments

tobske picture tobske  路  3Comments

davidbarratt picture davidbarratt  路  3Comments