Subrequests with post vars

一笑奈何 提交于 2020-01-03 06:33:10

问题


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:


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.



来源:https://stackoverflow.com/questions/25570086/subrequests-with-post-vars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!