Symfony2 fos login, register and forgot password in one view(Symfony2 fos登录,注册和忘记密码在一个视图中)
问题描述
我需要将购买的模板应用到我们的仪表板.在此模板中,登录、注册和忘记密码表单都在同一个视图下,并使用简单的JQuery在它们之间切换.
我一直在寻找一种将所有三种形式合二为一的好方法、不太浮夸的方法,但我一无所获.
我的常备选项(如我所见),以及为什么我不喜欢其中任何一个:
- 从 fos 包中获取视图,将它们复制到
/app/Resources/FOSUserBundle/views/
,删除{% extend %}
部分和{% include %}
在我自己的登录视图中.不喜欢的原因:在我看来,这有点像一个快速的-n-dirty 修复 - 那部分不起作用?让我们中断它吧!":) - 扩展 fos 包,在
LoginAction
和RegisterAction
中接受一个额外的参数,在我自己的参数中使用{% render %}
登录视图.不喜欢的原因:扩展整个包并修改两个不同的控制器只是为了改变它的呈现方式感觉就像糟糕的 MVC. - XHR 加载所有内容.不喜欢的原因:这种方法在使用内页时是有意义的,但对于无论如何都要重新加载的页面就没有意义了.
TL;DR 版本:我正在寻找一种在一个页面中包含登录、注册和忘记密码表单的非黑客方式.
任何帮助将不胜感激!
我找到了一个适合我当前项目的解决方案.预先提出的解决方案的优缺点:
优点:
- 需要实施的 LOC 很少
- FOSUserBundle 更新证明(不覆盖视图脚本*)
缺点:
- 子请求导致的性能开销
- 只能显示表单,表单提交(以及随后提交时的错误处理)将始终转到 FOSUserBundle 提供的页面
- 仍然感觉像是一个快速修复,但比其他选项更好
* 只需要覆盖 layout.html.twig 文件
<小时>话虽如此,这就是我所做的:
在模板中呈现表单
使用 嵌入式控制器 呈现您需要的表单:<h2>登录</h2>{{ render(controller('FOSUserBundle:Security:login', { embeddedForm: true})) }}</div><h2>重置</h2>{{ render(controller('FOSUserBundle:Resetting:request', { embeddedForm: true})) }}</div>覆盖 FOSUserBundle 布局
当我使用捆绑提供的路由时,我必须 覆盖 FOSUserBundle 布局模板文件 以扩展我的应用程序的标准布局.由于覆盖的 FOSUserBundle 布局文件扩展了主应用程序布局文件,因此每次调用{{ render ... }}
都会重复布局.为了防止这种情况,我们需要动态解除扩展布局文件.这是覆盖的布局文件的样子:{# app/Resources/FOSUserBundle/views/layout.html.twig #}{% if app.request.get('embeddedForm') %}{% 设置布局 = 'AcmeBundle::layout-content.html.twig' %}{% 别的 %}{% 设置布局 = 'AcmeBundle::layout.html.twig' %}{% 万一 %}{% 扩展布局 %}{% 块内容 %}{% 块 fos_user_content %}{% endblock %}{% 端块 %}
创建
AcmeBundle::layout-content.html.twig 文件
这种布局应该只呈现 FOSUserBundle 视图脚本的content
块,并且非常简短:{# src/Acme/DemoBundle/Resources/views/layout-content.html.twig #}{% 块内容 %}{% endblock %}
现在表单将很好地呈现所有依赖项(CSRF 等).但是,提交表单会将您带到 FOSUserBundle 操作.
<小时>替代解决方案:
- 这个答案描述了如何手动实现表单并将它们链接到 FOSUserBundle 控制器.
I need to apply a purchased template to our dashboard. In this template, the login, register and forgot password forms are all under the same view, and switching between them using simple JQuery.
I have been looking for a nice, not-too-flashy way of combining all three forms into one, but I came up empty.
My standing options (as I see them), and why I don't like any of them:
- Take the views from the fos bundle, copy them to
/app/Resources/FOSUserBundle/views/
, remove the{% extend %}
part and{% include %}
them in my own login view. Reason for dislike: to me this looks a little like a quick-n-dirty fix - "that part's not working? Let's break it off!" :) - Extend the fos bundle, accept an extra parameter in the
LoginAction
andRegisterAction
, use{% render %}
with parameters in my own login view. Reason for dislike: extending a whole bundle and modifying two different controllers just to change the way it renders feels like bad MVC. - XHR load everything. Reason for dislike: this approach makes sense when using inner pages, but for pages that reload anyway it just doesn't make sense.
TL;DR version: I'm looking for a non-hack way of including the login, register and forgot password form in one page.
Any help would be greatly appreciated!
解决方案I found a solution with which I am comfortable with for my current project. The advantages and disadvantages of the proposed solution upfront:
Advantages:
- few LOC to implement
- FOSUserBundle update proof (does not override the view scripts*)
Disadvantages:
- performance overhead due to subrequests
- only forms can be displayed, form submission (and subsequently error handling upon submission) will always go to the pages provided by FOSUserBundle
- still feels like a quick-n-dirty fix, but better than other options
* only needs to override the layout.html.twig file
With that being said, here is what I have done:
Render the form in your template
Use embedded controllers to render the forms you need:<div> <h2>Login</h2> {{ render(controller('FOSUserBundle:Security:login', { embeddedForm: true})) }} </div> <div> <h2>Reset</h2> {{ render(controller('FOSUserBundle:Resetting:request', { embeddedForm: true})) }} </div>
Override FOSUserBundle layout
As I use the routes provided by the bundle, I had to override the FOSUserBundle layout template file to extend the standard layout of my application. As the overriden FOSUserBundle layout file extends the main applications layout file the layout would be repeated for each call{{ render ... }}
. To prevent that, we need to dynamically disarm the extended layout file. Here is what the overriden layout file looks like:{# app/Resources/FOSUserBundle/views/layout.html.twig #} {% if app.request.get('embeddedForm') %} {% set layout = 'AcmeBundle::layout-content.html.twig' %} {% else %} {% set layout = 'AcmeBundle::layout.html.twig' %} {% endif %} {% extends layout %} {% block content %} {% block fos_user_content %}{% endblock %} {% endblock %}
Create the
AcmeBundle::layout-content.html.twig file
This layout should only render thecontent
block of the FOSUserBundle view scripts and is such short and simple:{# src/Acme/DemoBundle/Resources/views/layout-content.html.twig #} {% block content %}{% endblock %}
Now the forms will render nicely with all dependencies (CSRF and so forth). Submitting the form will however take you to the FOSUserBundle actions.
Alternative solution:
- This answer describes how to manually implement the forms and link them to the FOSUserBundle controller.
这篇关于Symfony2 fos登录,注册和忘记密码在一个视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Symfony2 fos登录,注册和忘记密码在一个视图中
基础教程推荐
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01