Symfony2 fos登录、注册和忘记密码在一个视图中。[英] Symfony2 fos login, register and forgot password in one view

本文是小编为大家收集整理的关于Symfony2 fos登录、注册和忘记密码在一个视图中。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我需要将购买的模板应用于我们的仪表板.在此模板中,登录,注册和忘记密码表格都在同一视图下,并使用简单的jQuery进行切换.

我一直在寻找一种将这三种形式组合到一张形式中的不错的,不太易变的方式,但是我空了.

我的站立选择(如我所见),以及为什么我不喜欢它们:

  1. 从FOS捆绑包中获取视图,将它们复制到/app/Resources/FOSUserBundle/views/,删除{% extend %}零件,然后在我自己的登录视图中删除{% include %}. 不喜欢的理由:对我来说,这看起来有点像一个快速的n-dirty修复程序 - "那部分不起作用吗?让我们将其分解!" :)
  2. 扩展FOS捆绑包,接受LoginAction和RegisterAction中的额外参数,在我自己的登录视图中使用{% render %}使用{% render %}. 不喜欢的原因:扩展了整个捆绑包并修改两个不同的控制器,只是为了改变其渲染方式,感觉就像MVC.
  3. XHR加载所有内容. 不喜欢的原因:使用内页时,这种方法是有意义的,但是对于重新加载的页面来说,这是没有意义的.

tl; dr版本:我正在寻找一种不包含登录,注册和忘记密码表单的方式.

任何帮助将不胜感激!

推荐答案

我找到了一个解决方案,我可以为当前的项目感到满意.预先提出的解决方案的优势和缺点:

优点:

  • 很少有实施的LOC
  • fosuserbundle更新证明(不覆盖视图脚本*)

缺点:

  • 由于子征询的性能开销
  • 只能显示表格,表单提交(以及提交时的错误处理)将始终转到FosuserBundle提供的页面
  • 仍然感觉像一个快速的修复程序,但比其他选项更好

*只需要覆盖layout.html.twig文件


话虽如此,这就是我所做的:

  1. 在模板中渲染表单
    使用嵌入式控制器启用您需要的表格:

    <div>
        <h2>Login</h2>
        {{ render(controller('FOSUserBundle:Security:login', { embeddedForm: true})) }}
    </div>
    <div>
        <h2>Reset</h2>
        {{ render(controller('FOSUserBundle:Resetting:request', { embeddedForm: true})) }}
    </div>
    
  2. 覆盖fosuserbundle布局
    As I use the routes provided by the bundle, I had to 覆盖fosuserbundle布局模板文件扩展了我的应用程序的标准布局.当Overriden fosuserbundle布局文件扩展了主应用程序布局文件时,每个调用{{ render ... }}将重复布局.为了防止这种情况,我们需要动态解除扩展布局文件.这是Overriden布局文件的样子:

    {# 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 %}
    
  3. 创建AcmeBundle::layout-content.html.twig file
    这种布局只能渲染fosuserbundle视图脚本的content块,并且如此简单:

    {# src/Acme/DemoBundle/Resources/views/layout-content.html.twig #}
    {% block content %}{% endblock %}
    

现在,这些表格将与所有依赖关系(CSRF等)呈现很好.但是,提交表格将带您进入FosuserBundle动作.


替代解决方案:

  • 这个答案描述了如何手动实现表格并将其链接到FosuserBundle Controller.

本文地址:https://www.itbaoku.cn/post/2090786.html

问题描述

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:

  1. 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!" :)
  2. Extend the fos bundle, accept an extra parameter in the LoginAction and RegisterAction, 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.
  3. 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:

  1. 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>
    
  2. 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 %}
    
  3. Create the AcmeBundle::layout-content.html.twig file
    This layout should only render the content 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.