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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应