带有 post vars 的子请求 [英] Subrequests with post vars

查看:70
本文介绍了带有 post vars 的子请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是大图:我正在为我的 Web 应用程序编写一个 symfony2 包.此应用程序包含在带有 CRUD 控制器的标准网站中.另一方面,它包含一个 rest api,它也管理实体的创建/编辑/....

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.

我开始为 User 实体编写 Rest\UserController.它包含所有标准 REST 操作(GET、POST、PUT、DELETE).它基于 William Durand 的非常好的教程:http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/

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/

创建并运行后,我创建了另一个 UserController 来处理应用程序的 Web 端.在这个控制器中,我有一个名为 editAction 的操作,它在 HTML 响应中呈现一个表单.此表单在提交时向同一控制器的操作 putAction 发送 PUT 请求.我的想法是将请求转发到 Rest\UserController 并使用 putAction 操作.下面是 UserController::putAction 的代码:

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

这就像一种魅力,感觉这是实现它的好方法.当我认为我会为用户激活/停用做同样的事情时,问题就出现了.我的 UserController 中有一个 lockAction,它会通过Rest\UserController::putAction"和合成数据运行请求,以更改启用的字段.

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.

但我的问题是似乎无法在前向调用中设置 POST 变量(只有路径和查询).我什至尝试将 $kernel->handle($request) 与合成请求一起使用,但它没有找到我的 Rest 控制器的路由.

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.

我错过了什么吗?

推荐答案

我不确定这是否有效,但您可以尝试一下.

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

所以重复的方法将采用一个post变量数组.因此,请尝试以下操作:

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

想知道这是否有效.我总是将我的 REST 设置为一个单独的应用程序,然后使用 guzzle 来连接它.但是转发会更快.

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.

这篇关于带有 post vars 的子请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆