使用构造函数参数将请求转发给另一个控制器

时间:2016-01-14 12:06:30

标签: symfony

在另一个控制器中调用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 转发请求时是否可以传递构造函数参数?或者有不同的方式吗?目前我收到的错误如下。我在documentation中没有看到任何示例。

<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>

service.xml中

/**
 * @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);
    }
}

ReceiverController

/**
 * @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);
    }
}

SenderController

int (*b)[100]

1 个答案:

答案 0 :(得分:3)

由于您的控制器已注册为服务,因此必须使用相同的表示法来指代配置路由时使用的控制器。这意味着您应该使用MyOrderApiBundle:Receiver:givItToMeAction而不是my_api.controller.receiver:givItToMeAction