Symfony Server crashes when extending FOSUserBundl

2019-02-27 16:56发布

问题:

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.

回答1:

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.



回答2:

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.