FOSUserBundle:嵌入登录表单并选择其模板 [英] FOSUserBundle: embedding the login form and choosing its template

查看:24
本文介绍了FOSUserBundle:嵌入登录表单并选择其模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的模板中插入 FOSUserBundle 的登录表单,例如这个:

但不渲染在代码中调用的模板loginAction() 原本.

我认为我会发现传递模板的可能性很有用在此示例中,我想将参数渲染为max":

这在 symfony2 中可能吗?如果没有..

我应该为我的包创建另一个动作,里面有相同的代码吗?登录操作?或者我应该修改原始loginAction代码并编写控件结构?

if(currentPage == 'home')渲染响应('模板1')别的渲染响应('模板2')

解决方案

您可以创建一个部分来存储您的纯 HTML 表单,例如这个简单的表单:

<label>电子邮件</label><input type="text" name="_username" required="required"/><label>密码</label><input type="password" name="_password" required="required"/><button type="submit">提交</button></表单>

然后在您的布局中只包含您全新的部分 :)

如果您需要 csrf 令牌(强烈推荐),只需将提供程序定义为 Twig 全局变量:

树枝:全局变量:fos_csrf_provider: "@form.csrf_provider"

然后只需在表单中添加隐藏的输入字段:

 

i want to insert the login form of FOSUserBundle in my template like this:

<div id="sidebar">
     {% render "FOSUserBundle::Security::login" %}
</div> 

but not to render the template that is been calling in the code of loginAction() originally.

I have thought I'd find useful the possibility of passing the template I want to render as a parameter as 'max' in this example:

<div id="sidebar">
    {% render "AcmeArticleBundle:Article:recentArticles" with {'max':
3} %}
</div>

Is that possible in symfony2? If not..

should I create another action for my bundle with the same code inside of loginAction? or should I modify the original loginAction code and write control structures?

if(currentPage == 'home')
 renderResponse('template1')
else
renderResponse('template2')

解决方案

You can create a partial to store your plain HTML form, for example this simple one:

<form action="{{ path("fos_user_security_check") }}" method="post">
  <label>Email</label>
  <input type="text" name="_username" required="required" />

  <label>Password</label>
  <input type="password" name="_password" required="required" />

  <button type="submit">Submit</button>
</form>

Then in your layout just include your brand new partial :)

If you need the csrf token (which is highly recommended), just define the provider as a Twig global variable:

twig:
    globals:
        fos_csrf_provider: "@form.csrf_provider"

Then just add the hidden input field in your form:

  <input type="hidden" name="_csrf_token" value="{{ fos_csrf_provider.generateCsrfToken('authenticate') }}" />

这篇关于FOSUserBundle:嵌入登录表单并选择其模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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