当扩展FOSUserBundle'的默认模板layout.html.twig时,Symfony服务器崩溃了。[英] Symfony Server crashes when extending FOSUserBundle's default template layout.html.twig

本文是小编为大家收集整理的关于当扩展FOSUserBundle'的默认模板layout.html.twig时,Symfony服务器崩溃了。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试从FosuserBundle中获得基本的用户登录. 我正在使用Symfony 3.0.6.

我遵循描述来设置FosuserBundle中的所有内容: https://symfony.com/doc/doc/doc/master/master/bundles/fosuserbundles/fosuserbundle/index.html.html

db正在启动,并且运行所有内容似乎还不错,除了我无法弄清楚如何从fosuserbundle中覆盖layout.html.twig. 我遵循此描述来实现: https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_templates.html

我现在在文件夹"资源/fosuserbundle/views"中有一个文件" layout.html.twig",内容与上面提供的最后一个链接相同. 这导致以下错误:

无法找到模板" layout.html.twig"(查看: [somepathinfo])在fosuserbundle :: layout.html.twig在第1行.

现在,我更改了" Layout.html.twig"模板中的第一行,为

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

然后,这导致Symfony服务器崩溃

>php bin/console server:run -v                                                                                                                       
 [OK] Server running on http://127.0.0.1:8000                                                                           
 // Quit the server with CONTROL-C.
  RUN  "C:\xampp\php\php.exe" "-S" "127.0.0.1:8000" "[PATH]\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\config\router_dev.php"
  RES  -1073741571 Command did not run successfully
 [ERROR] Built-in server terminated unexpectedly.

我被困在这里... 任何想法都非常欢迎.

编辑:由作曲家安装的FosuserBundle(我通过当前PHP-STORM插件使用)已安装在路径上:

[projectPath]\vendor\friendsofsymfony\user-bundle\Resources\views\layout.html.twig

在文档中,仅提及" fosuserbundle",我不知道如何弄清楚该映射是否适合我项目的路径. 这个问题的任何提示也非常好.

推荐答案

当您覆盖Standart Fosuser布局时,您需要将布局放入app/Resources/FOSUserBundle/views/layout.html.twig中.太好了,你做到了.它只是一个布局,不应扩展Standart Fosuser布局,因此请删除行{% extends 'FOSUserBundle::layout.html.twig' %}.但是通常开发人员会制作一个基本布局,就我而言,它是\app\Resources\views\base.html.twig,因此,如果我想覆盖fosuser布局,我将在app/Resources/FOSUserBundle/views/layout.html.twig中拥有类似的东西

{% extends 'base.html.twig' %}

{% block title %}User Management{% endblock %}

{% block content %}
    {% block fos_user_content %}{% endblock %}
{% endblock %}

在第一行中,您扩展了基本布局而不是fosuser.您可能不会扩展某些内容,也许您对此模板有单独的完整布局.

其他推荐答案

崩溃确实有意义.

写作:

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

Symfony将首先尝试加载app/Resources/FOSUserBundle/views/layout.html.twig.未能找到该文件将恢复到类似的路径,但在vendor目录内.而且,如果您试图从fos Overriden模板中扩展FOS的模板,那将创建递归循环:

app/Resource/FOSUserBundle/views/layout.html.twig 
^^ extends  
app/Resource/FOSUserBundle/views/layout.html.twig
^^ extends
app/Resource/FOSUserBundle/views/layout.html.twig
....
and so on...

因此,这不是解决问题的方法.

确保您的模板很好地放置在您的app目录中,而不是捆绑包中,如丹尼斯·阿里莫夫(Denis Alimov)在评论中所建议的那样.

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

问题描述

I am trying to get the basic user login running from the FOSUserBundle. I am using Symfony 3.0.6.

I followed the description to setup everything from the FOSUserBundle: https://symfony.com/doc/master/bundles/FOSUserBundle/index.html

DB is up and running everything seems fine except I cant figure out how to override the layout.html.twig from the FOSUserBundle. I followed this description for achieving that: https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_templates.html

I now have a file "layout.html.twig" in the folder "Resources/FOSUserBundle/views" with the content being the same as in the last link provided above. This leads to the following error:

Unable to find template "layout.html.twig" (looked into: [somePathInfo]) in FOSUserBundle::layout.html.twig at line 1.

Now I changed the first line in the "layout.html.twig" Template to be

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

And this then leads to the symfony server to crash stating

>php bin/console server:run -v                                                                                                                       
 [OK] Server running on http://127.0.0.1:8000                                                                           
 // Quit the server with CONTROL-C.
  RUN  "C:\xampp\php\php.exe" "-S" "127.0.0.1:8000" "[PATH]\myProject\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\config\router_dev.php"
  RES  -1073741571 Command did not run successfully
 [ERROR] Built-in server terminated unexpectedly.

I am stuck here... Any ideas are very welcome.

EDIT: The FOSUserBundle installed by the composer (which I use through the current PHP-Storm plugin) is installed at the path:

[projectPath]\vendor\friendsofsymfony\user-bundle\Resources\views\layout.html.twig

In the docu however allways "FOSUserBundle" only is mentioned and I don't know how to figure out if that mapping fits to the path in my project. Any hints for this issue are very wellcome as well.

推荐答案

When you override standart FOSUser layout you need to place your layout into app/Resources/FOSUserBundle/views/layout.html.twig. great, you did this. it's just a layout, it should not extend standart FOSUser layout, so remove line {% extends 'FOSUserBundle::layout.html.twig' %}. But usually developers make one base layout, in my case it is \app\Resources\views\base.html.twig, so if I want to override fosuser layout I will have in app/Resources/FOSUserBundle/views/layout.html.twig something like this

{% extends 'base.html.twig' %}

{% block title %}User Management{% endblock %}

{% block content %}
    {% block fos_user_content %}{% endblock %}
{% endblock %}

In first line you extend your base layout not FOSUser. You may not extend something, maybe you have separate complete layout for this template.

其他推荐答案

The crash does make sense.

When you write:

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

The Symfony will first try to load app/Resources/FOSUserBundle/views/layout.html.twig. Failing to find the file will revert to similar path but inside the vendor directory. And if you are trying to extend FOS's template from within your FOS overriden template, that would create recursive loop:

app/Resource/FOSUserBundle/views/layout.html.twig 
^^ extends  
app/Resource/FOSUserBundle/views/layout.html.twig
^^ extends
app/Resource/FOSUserBundle/views/layout.html.twig
....
and so on...

So, this is not a way to solve the problem.

Make sure that your template is well placed in your app directory and not your bundle, as Denis Alimov suggested in a comment.