Symfony的2形式的嵌入式控制器和AJAX [英] Symfony 2 form in embedded controller and AJAX

查看:118
本文介绍了Symfony的2形式的嵌入式控制器和AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在symfony中2所示,一个可以嵌入在模板中的控制器。例如,我可能对博客文章的模板,但我也可以嵌入控制器来投票或顶部文章模板的边栏的列表。

In symfony 2, one can embed a controller in a template. For example, I might have a template for a blog post, but I can also embed a controller to a poll or a list of top articles in the side-bar of the template.

我感兴趣的嵌入控制器,提供了一个民意调查。要做到这一点,我已经创建了一个控制器:

I am interested in embedding a controller that provides a poll. To do so, I have created a controller:

public function poll(Request $request)
{
    $task = new Poll();

    $form = $this->createFormBuilder($task)
        ->add('radio', 'one')
        ->add('radio', 'two')
        ->add('radio', 'three')
        ->getForm();

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            //save and show how many people voted for each option
        }
    }

    return $this->render('PollBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));

}

然后我可以嵌入这个作为我的模板侧栏项目:

I can then embed this as a side bar item in my template:

<div id="sidebar">
    {% render "PollBundle:Poll:poll" %}
</div>

现在的问题是这样的: 如果我设置表单动作要,那么如果调查被提交,该请求将通过其内嵌的投票控制器的主控制器。那么我可以用一个隐藏字段来检查,如果调查是通过投票的形式提交,然后更新数据库和渲染的结果。这然后被返回到主控制器的模板和所有良好。

The problem now is this: If I set the form action to be "", then if the poll was submitted, the request would go through the main controller which embeds the Poll controller. I can then use a hidden field to check if the poll was submitted using the poll form, then update the database and render the result. This then gets returned to the template of the main controller and all is good.

我现在想使用一些AJAX来简化表单提交对于那些谁安装Javascript。我怎么能去这样做?由于形式操作设置为,该请求将通过主控制器,渲染,然后调用轮询控制器的模板。不过,我想只返回包含铸造每个项目的票数Ajax响应。在anycase,不必经过主控制器只使用一个AJAX请求seesm是相当浪费太多填充的一项民意调查。

I would now like to use some AJAX to streamline the form submission for those who have javascript enabled. How can I go about doing this? Since the form action is set to "", the request would go through the main controller, render the template which then calls the Poll controller. But I would like to just return an AJAX response containing the votes casted for each item. In anycase, having to go through the main controller just to populate a poll using an AJAX request seesm to be quite wasteful too.

我应该如何去这样做?

推荐答案

您可以点形式到它自己的行动来处理后,如果它被通过AJAX请求,然后返回结果,或者,如果是通过正常的达成请求重定向到HTTP_REFERER($这个 - >获得(请求) - >服务器 - >获得(HTTP_REFERER))。

You can point the form to its own action that handles the post and, if it's being requested through ajax then return the results or, if it's reached through a normal request, redirect to the HTTP_REFERER ( $this->get('request')->server->get('HTTP_REFERER') ).

如果你不想依赖引用者,你也可以渲染投票(在你上面所描述的pollAction)时存储当前的URI的会话变量。

If you don't want to rely on the referer, you could also store the current URI in a session variable when rendering the poll (in the pollAction you've described above).

这篇关于Symfony的2形式的嵌入式控制器和AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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