doumibi6899 2014-08-29 14:11
浏览 45
已采纳

带有后期变量的子请求

Here's the big picture: I am writing a symfony2 bundle for my web application. This application consists in a standard website with CRUD controllers. And on the other side it contains a rest api that also manages creation/edit/... on entities.

I started writing the Rest\UserController for the User entity. It contains all standard REST actions (GET, POST, PUT, DELETE). It is based on the very good tutorial by William Durand: http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/

Once this was created and functional I have created another UserController to handle the web side of the application. In this controller I have an action called editAction that renders a form in an HTML response. This form, when submitted sends a PUT request to the same controller's action putAction. My idea was to forward the request to Rest\UserController with action putAction. Here is the code for UserController::putAction:

    /**
     * This action forwards the request to the REST controller. It redirects
     * to a user list upon success, or displays the message should an error
     * occur.
     * @Route("/{id}/put", name="igt_user_put")
     * @Method("PUT")
     */
    public function putAction (User $user)
    {
        $response = $this->forward('MyBundle:Rest\User:put', array('id'=>$user->getId()));
        if ($response->getStatusCode() == Response::HTTP_NO_CONTENT) {
            return new RedirectResponse($this->generateUrl('igt_user_list'));
        }
        return $response;
    }

This works like a charm and it feels like it is the good way to do it. The problem occurred when I thought I'd do the same for user activation/deactivation. I'd have a lockAction in my UserController that would run a request through the "Rest\UserController::putAction` with synthetic data to change the enabled field.

But my problem is that there seems to be no way to set the POST vars in the forward call (only path and query). I even tried using $kernel->handle($request) with a synthetic request but it doesn't find my Rest controller's routes.

Am I missing something ?

  • 写回答

1条回答 默认 最新

  • dongran1779 2014-08-29 14:45
    关注

    I'm not sure of this will work or not but you could try it.

    // Framework controller class
    public function forward($controller, array $path = array(), array $query = array())
    {
        $path['_controller'] = $controller;
        $subRequest = $this->container->get('request_stack')
            ->getCurrentRequest()->duplicate($query, null, $path);
    
        return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
    }
    

    We can see that it duplicates the current request and then handles it.

    // Request
    /**
     * Clones a request and overrides some of its parameters.
     *
     * @param array $query      The GET parameters
     * @param array $request    The POST parameters
     * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
     * ...
     */
    public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
    {
    

    So the duplicate method will take an array of post variables. So try something like:

    public function forwardPost($controller, 
        array $path = array(), 
        array $query = array(), 
        array $post = array())
    {
        $path['_controller'] = $controller;
        $subRequest = $this->container->get('request_stack')
            ->getCurrentRequest()->duplicate($query, $post, $path);
    
        return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
    }
    

    Be curious to see if this will work. I always just set my REST up as a separate application and then use guzzle to interface to it. But forwarding would be faster.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私