Symfony2 FOSUserBundle 重写表单

发布于 2025-01-04 00:56:52 字数 4280 浏览 0 评论 0原文

我正在尝试更改应用程序中注册表单的模板,以便我可以向其中添加一些其他 HTML。这是 /My/UserBundle/Resources/views/Registration/register.html.twig 文件:

{% extends "MyUserBundle::layout.html.twig" %}

{% block fos_user_content %}
<section class="register site-content">
    <header>
        <h1>{{ 'layout.register'|trans({}, 'FOSUserBundle') }}</h1>
    </header>
    <div class="block">
        {% include "FOSUserBundle:Registration:register_content.html.twig" %}
    </div>
</section>
{% endblock fos_user_content %}

并且我已成功覆盖layout.html.twig:

{% extends 'MyMainBundle::layout.html.twig' %}

{% block title %}{{ site_name }}{% endblock %}

{% block content %}
    {% for key, message in app.session.getFlashes() %}
    <div class="{{ key }}">
        {{ message|trans({}, 'FOSUserBundle') }}
    </div>
    {% endfor %}
    {% block fos_user_content %}{% endblock %}
{% endblock %}

以及 form.html.twig:

{% extends 'FOSUserBundle::form.html.twig' %}

{% block field_row %}
    <li class="form_row">
        {{ form_label(form) }}
        {{ form_errors(form) }}
        {{ form_widget(form) }}
    </li>
{% endblock field_row %}

{% block form_widget %}
    <ul {{ block('container_attributes') }}>
        {{ block('field_rows') }}
        {{ form_rest(form) }}
    </ul>
{% endblock form_widget %}

配置部分:

# FOS User Configuration
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: My\UserBundle\Entity\User
    from_email:
      address: %admin_email%
      sender_name: %site_name%
    template:
      engine: twig
      theme: MyUserBundle::form.html.twig

我已清除缓存。

每当我访问

http://localhost/register/

apache 就会挂起,直到超时。

我能弄清楚的最好消息是,PHP 最大执行消息显示它在第 16 行缓存中的树枝模板上崩溃。该行是 function doGetParent(...) 该文件是:

<?php

/* FOSUserBundle::form.html.twig */
class __TwigTemplate_9cf68a2af1db50466c556a735bcdeba0 extends Twig_Template
{
    public function __construct(Twig_Environment $env)
    {
        parent::__construct($env);

        $this->blocks = array(
            'field_row' => array($this, 'block_field_row'),
            'form_widget' => array($this, 'block_form_widget'),
        );
    }

    protected function doGetParent(array $context)
    {
        return "FOSUserBundle::form.html.twig";
    }

    protected function doDisplay(array $context, array $blocks = array())
    {
        $this->getParent($context)->display($context, array_merge($this->blocks, $blocks));
    }

    // line 3
    public function block_field_row($context, array $blocks = array())
    {
        // line 4
        echo "    <li class=\"form_row\">
        ";
        // line 5
        echo $this->env->getExtension('form')->renderLabel($this->getContext($context, "form"));
        echo "
        ";
        // line 6
        echo $this->env->getExtension('form')->renderErrors($this->getContext($context, "form"));
        echo "
        ";
        // line 7
        echo $this->env->getExtension('form')->renderWidget($this->getContext($context, "form"));
        echo "
    </li>
";
    }

    // line 11
    public function block_form_widget($context, array $blocks = array())
    {
        // line 12
        echo "    <ul ";
        $this->displayBlock("container_attributes", $context, $blocks);
        echo ">
        ";
        // line 13
        $this->displayBlock("field_rows", $context, $blocks);
        echo "
        ";
        // line 14
        echo $this->env->getExtension('form')->renderRest($this->getContext($context, "form"));
        echo "
    </ul>
";
    }

    public function getTemplateName()
    {
        return "FOSUserBundle::form.html.twig";
    }

    public function isTraitable()
    {
        return false;
    }
}

它在 \vendor\twig\lib\Twig\Template.php 上also 超时第 65 行 这是 public function getParent(array $context)

显然 getParent 存在一些问题,但我不知道这意味着什么或如何修复它。

I am trying to change the template for the registration form in my application so that I can add some other HTML to it. Here is the /My/UserBundle/Resources/views/Registration/register.html.twig file:

{% extends "MyUserBundle::layout.html.twig" %}

{% block fos_user_content %}
<section class="register site-content">
    <header>
        <h1>{{ 'layout.register'|trans({}, 'FOSUserBundle') }}</h1>
    </header>
    <div class="block">
        {% include "FOSUserBundle:Registration:register_content.html.twig" %}
    </div>
</section>
{% endblock fos_user_content %}

And I have successfully overridden the layout.html.twig:

{% extends 'MyMainBundle::layout.html.twig' %}

{% block title %}{{ site_name }}{% endblock %}

{% block content %}
    {% for key, message in app.session.getFlashes() %}
    <div class="{{ key }}">
        {{ message|trans({}, 'FOSUserBundle') }}
    </div>
    {% endfor %}
    {% block fos_user_content %}{% endblock %}
{% endblock %}

as well as form.html.twig:

{% extends 'FOSUserBundle::form.html.twig' %}

{% block field_row %}
    <li class="form_row">
        {{ form_label(form) }}
        {{ form_errors(form) }}
        {{ form_widget(form) }}
    </li>
{% endblock field_row %}

{% block form_widget %}
    <ul {{ block('container_attributes') }}>
        {{ block('field_rows') }}
        {{ form_rest(form) }}
    </ul>
{% endblock form_widget %}

config parts:

# FOS User Configuration
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: My\UserBundle\Entity\User
    from_email:
      address: %admin_email%
      sender_name: %site_name%
    template:
      engine: twig
      theme: MyUserBundle::form.html.twig

I have cleared my cache.

Whenever I go to

http://localhost/register/

apache just hangs until it times out.

The best I can figure out, is the PHP maximum execution message says it crashes on a twig template in the cache on line 16. That line is function doGetParent(...) The file is:

<?php

/* FOSUserBundle::form.html.twig */
class __TwigTemplate_9cf68a2af1db50466c556a735bcdeba0 extends Twig_Template
{
    public function __construct(Twig_Environment $env)
    {
        parent::__construct($env);

        $this->blocks = array(
            'field_row' => array($this, 'block_field_row'),
            'form_widget' => array($this, 'block_form_widget'),
        );
    }

    protected function doGetParent(array $context)
    {
        return "FOSUserBundle::form.html.twig";
    }

    protected function doDisplay(array $context, array $blocks = array())
    {
        $this->getParent($context)->display($context, array_merge($this->blocks, $blocks));
    }

    // line 3
    public function block_field_row($context, array $blocks = array())
    {
        // line 4
        echo "    <li class=\"form_row\">
        ";
        // line 5
        echo $this->env->getExtension('form')->renderLabel($this->getContext($context, "form"));
        echo "
        ";
        // line 6
        echo $this->env->getExtension('form')->renderErrors($this->getContext($context, "form"));
        echo "
        ";
        // line 7
        echo $this->env->getExtension('form')->renderWidget($this->getContext($context, "form"));
        echo "
    </li>
";
    }

    // line 11
    public function block_form_widget($context, array $blocks = array())
    {
        // line 12
        echo "    <ul ";
        $this->displayBlock("container_attributes", $context, $blocks);
        echo ">
        ";
        // line 13
        $this->displayBlock("field_rows", $context, $blocks);
        echo "
        ";
        // line 14
        echo $this->env->getExtension('form')->renderRest($this->getContext($context, "form"));
        echo "
    </ul>
";
    }

    public function getTemplateName()
    {
        return "FOSUserBundle::form.html.twig";
    }

    public function isTraitable()
    {
        return false;
    }
}

It has also timed out on \vendor\twig\lib\Twig\Template.php on line 65 Which is public function getParent(array $context)

So clearly there is some problem with getParent but I don't know what that means or how to fix it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱我好吗 2025-01-11 00:56:52

根据 FOSUserBundle 文档:

覆盖捆绑包模板的最简单方法是简单地放置一个
在您的 app/Resources 文件夹中新建一个。覆盖布局模板
位于 FOSUserBundle 中的 Resources/views/layout.html.twig
目录,您可以将新的布局模板放置在
app/Resources/FOSUserBundle/views/layout.html.twig

正如您所看到的,以这种方式覆盖模板的模式是
app/Resources中创建一个具有捆绑包类名称的文件夹
目录。然后将新模板添加到此文件夹中,保留
原始包的目录结构。

在我的项目中,我按照他们所说的覆盖了 FOSUserBundle 的布局,它的工作就像一个魅力。

因此,您需要以同样的方式创建app/Resources/FOSUserBundle/views/Registration/register.html.twig。 (或您想要覆盖的表单)

编辑

好的,我刚刚意识到您已经选择扩展 FOSUserBundle。在这种情况下,您需要在包内执行此操作,而不是 app/Resources/ 。但您不需要放置

{% extends 'FOSUserBundle::form.html.twig' %}

FOSUserBundle 会检测到您正在覆盖该捆绑包并将自动扩展。

您还需要告诉您的包 FOSUserBundle 是它的父包。

class YourBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

According to the FOSUserBundle documentation:

The easiest way to override a bundle's template is to simply place a
new one in your app/Resources folder. To override the layout template
located at Resources/views/layout.html.twig in the FOSUserBundle
directory, you would place you new layout template at
app/Resources/FOSUserBundle/views/layout.html.twig.

As you can see the pattern for overriding templates in this way is to
create a folder with the name of the bundle class in the app/Resources
directory. Then add your new template to this folder, preserving the
directory structure from the original bundle.

In my project I override FOSUserBundle's layout as they said and it's work like a charm.

So doing it at the same way you will need to create app/Resources/FOSUserBundle/views/Registration/register.html.twig. (or the form you want to override)

EDIT

Ok, I just realize that you've chosen to extend the FOSUserBundle. In that case instead of app/Resources/ you need to do it inside your bundle. But you don't need to put

{% extends 'FOSUserBundle::form.html.twig' %}

The FOSUserBundle will detect that you are overriding the bundle and will be extended automatically.

And you also need to tell your bundle that FOSUserBundle is its parent.

class YourBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}
明月松间行 2025-01-11 00:56:52

将文件复制

{% include "FOSUserBundle:Registration:register_content.html.twig" %}

到:app/Resources/FOSUserBundle/views/ 目录。然后它将覆盖基本 FOS 表单。您可以编辑复制的文件。

否则,您必须覆盖 FOS RegisterController。

Copy the file:

{% include "FOSUserBundle:Registration:register_content.html.twig" %}

to the: app/Resources/FOSUserBundle/views/ catalogue. Then it will overwrite the base FOS form. You can edit the copied file.

Otherwise you must override the FOS RegisterController.

千年*琉璃梦 2025-01-11 00:56:52

我觉得很愚蠢,只需删除 form.html.twig 中的 {% extends ... %} 行即可。我想我对捆绑中继承的内容还不够了解。 (我确实想从 FOSUserBundle form.html.twig 继承,但我不知道如何从从 FOSUserBundle 继承的包中访问它)

I feel stupid, just removed the {% extends ... %} line in form.html.twig and it worked. I guess I don't understand enough about what is inherited in bundles. (I did want to inherit from the FOSUserBundle form.html.twig, but I don't know how to access that from a bundle which inherits from FOSUserBundle)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文