天道酬勤,学无止境

Symfony Server crashes when extending FOSUserBundle's default template layout.html.twig

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.

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • 使用 cron 作业触发 URL(Trigger an URL with a cron job)
    问题 我正在 Symfony 3 网站上工作,我需要使用 cron 作业调用我网站的 URL。 我的网站托管在 OVH 上,我可以在其中配置我的 cron 作业。 现在,我已经设置了命令: ./demo/Becowo/batch/emailNewUser.php emailNewUser.php 内容: <?php header("Location: https://demo.becowo.com/email/newusers"); ?> 在日志中我有: [2017-03-07 08:08:04] ## OVH ## END - 2017-03-07 08:08:04.448008 exitcode: 0 [2017-03-07 09:08:03] ## OVH ## 开始 - 2017-03-07 09:08:03.988105 执行:/usr/local/php5.6/bin/php /homez.2332/coworkinwq/ ./demo/Becowo/batch/emailNewUser.php 但是不发送电子邮件。 我应该如何配置我的 cron 作业来执行这个 URL? 或者我应该直接调用我的控制器? 如何 ? 回答1 好的,终于成功了!!! 这是我为其他人遵循的步骤: 1/ 您需要一个控制器来发送电子邮件: 由于控制器将通过命令调用,因此您需要注入一些服务 em
  • Symfony 2 production app unable to find any Twig template
    I made Symfony 2 app what is working well on production and dev mode in my localhost server, but when i pushed it live to my ftp there was a problem. I checked log's and it's looks like Twig has problems with finding templates. There is log from my server: [2015-05-18 11:10:21] request.INFO: Matched route "homepage" (parameters: "_controller": "AppBundle\Controller\DefaultController::indexAction", "_route": "homepage") [] [] [2015-05-18 11:10:21] security.INFO: Populated SecurityContext with an anonymous Token [] [] [2015-05-18 11:10:21] request.CRITICAL: Uncaught PHP Exception
  • 是什么导致Symfony 2.1和Twig中出现“最大函数嵌套级别”错误?(What is causing a “Maximum function nesting level” error in Symfony 2.1 and Twig?)
    问题 我有一个关于Symfony 2.1的项目。 更新作曲家的组件(Gedemo,Symfony核心,Doctrine,Twig等。)之后,出现以下错误: Fatal error: Maximum function nesting level of '100' reached, aborting! in /var/www/{path}/vendor/twig/twig/lib/Twig/Token.php on line 78 我有PHP 5.4。 什么会导致此错误? 回答1 查找xdebug.ini文件: $ locate xdebug.ini /etc/php5/conf.d/20-xdebug.ini /etc/php5/mods-available/xdebug.ini 在我的情况下,文件为/etc/php5/conf.d/20-xdebug.ini 。 打开它并添加以下行: xdebug.max_nesting_level = 1000 不要忘记重启FPM服务器。 回答2 它是导致无限循环的错误代码,但有时会发生无错误的处理超过100个调用嵌套函数的情况。 若要更正此问题,请打开php.ini,xdebug部分,并添加以下行(输入您想要的值而不是150) [xdebug] xdebug.max_nesting_level = 150 回答3
  • 无法安装分析器 symfony flex(Can't install profiler symfony flex)
    问题 我在运行composer req profiler profiler 后无法使用 symfony profiler 我在 4.0 beta 版中使用 symfony/skeleton,但我记得我几个月前在 3.3 中测试时遇到了同样的问题。 工具栏不起作用,请在此处发送消息: 加载 Web 调试工具栏时出错。 打开网络分析器。 当单击“打开网络分析器”时,出现 404 错误。 如果有人解决了,谢谢你的帮助。 如果没有,我会在这仍处于测试阶段时报告错误。 关于安装:使用composer创建了一个项目,4.0-beta版中的symfony/skeleton包链接在这里:https://github.com/symfony/skeleton 我的 php 版本是 7.1 ; 我尝试从上面的命令安装分析器。 回答1 尝试执行以下步骤: composer create-project -s beta symfony/skeleton:4.0.x s40b1 cd s40b1 composer require web-server composer require cli composer require profiler 添加控制器 class DemoController extends AbstractController { public function demoAction(
  • Failed user login on production server using Symfony framework (Authentication request could not be processed due to…)
    I'am using Symfony for a project and I have been trying to get the login to work on production server with no success for the past 2 days. I keep getting the error Authentication request could not be processed due to a system problem. I have followed the guide here (http://symfony.com/doc/current/cookbook/security/entity_provider.html) to setup loading users from database. My security.yml file: security: encoders: Symfony\Component\Security\Core\User\User: plaintext Acceptme\UserBundle\Entity\User: plaintext role_hierarchy: ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] providers: in
  • Symfony2:Twig:自定义位置中的默认模板文件(Symfony2: Twig: Default template file in custom location)
    问题 我尝试加载一个简单的base.html.twig模板文件,该文件从 symfony 的默认位置app/Resources/views/到自定义位置theme/ 。 模板文件包含: <!DOCTYPE html> <html> <head> ... </head> <body> {% block body %}{% endblock %} </body> </html> 使用控制器专用模板通过控制器Acme\Core\CoreBundle\Controller扩展上述模板文件 {% extends '::base.html.twig' %} {% block body %} Hello world! {% endblock %} 导致错误说Unable to find template "::base.html.twig" in "AcmeCoreCoreBundle:Default:index.html.twig". . 如何告诉 symfony 在哪里可以找到全局空间中的模板文件? 提前致谢。 回答1 有一个本机功能可以以一种很好的方式做你想做的事情。 本质上,您可以像这样在 app/config.yml 中的 twig 配置中添加一个 twig 命名空间: twig: # ... paths: "%kernel.root_dir%/../vendor/acme/foo
  • Symfony2:找不到“GET /lucky/number”的路由(Symfony2: No route found for “GET /lucky/number”)
    问题 我开始教程(作为新手)并且一切正常,直到: http://symfony.com/doc/current/book/page_creation.html#creating-a-page-route-and-controller 在步骤创建页面:路由和控制器 我创建了一个名为/var/www/html/[projekt]/src/AppBundle/Controller/LuckyController.php的文件 但是当我打开 http://[Server-IP]:8000/app_dev.php/lucky/number 时总是得到 404: No route found for "GET /lucky/number" 404 Not Found - NotFoundHttpException 1 linked Exception: ResourceNotFoundException » [2/2] NotFoundHttpException: No route found for "GET /lucky/number" + [1/2] ResourceNotFoundException: + 路由.yml app: resource: "@AppBundle/Controller/" type: annotation 控制器 namespace AppBundle
  • FOSUserBundle-尝试访问login_path时如何重定向已登录的用户(FOSUserBundle - How to redirect already logged-in users when trying to access the login_path)
    问题 是否可以仅对经过AUTHENTICATED用户执行自动重定向到特定路由/login的某个路由(即/)? 如何? 我正在使用FOSUserBundle。 这是我的安全配置: security: encoders: FOS\UserBundle\Model\UserInterface: sha512 role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: ROLE_ADMIN providers: fos_userbundle: id: fos_user.user_provider.username_email firewalls: main: pattern: ^/ form_login: provider: fos_userbundle csrf_provider: form.csrf_provider login_path: /accedi check_path: /login_check default_target_path: / oauth: resource_owners: facebook: "/login/check-facebook" google: "/login/check-google" login_path: /accedi failure_path: /accedi default_target
  • Render template into Symfony2 with ajax
    I have a action in my controller for the index route routing.yml index: pattern: /index defaults: { _controller:AcmeDemoBundle:Default:index } Controller for this path public function indexAction() { return $this->render('AcmeDemoBundle:Plugin:index.html.twig'); } And the index.html.twig template {% extends'::base.html.twig' %} {% block stylesheets %} {% stylesheets filter='cssrewrite' output='css/*.css' 'bundles/acmedemo/css/*' %} <link href="{{ asset_url }}" type="text/css" rel="stylesheet" /> {% endstylesheets %} {% endblock stylesheets %} {% block body %} <br> <div class="container"> <div
  • Symfony2 将上传的文件分配到内存(Symfony2 allocates uploaded file to memory)
    问题 在 Symfony 中上传大于post_max_size的文件时,上传的文件被分配在内存中。 致命错误:第 28 行的 /Applications/MAMP/htdocs/Symfony/vendor/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php 中允许的内存大小为 150994944 字节(试图分配 84627994 字节) 为什么 symfony 试图在 POST 时将文件分配到内存? 1/ php.ini file_uploads = On upload_tmp_dir = /Applications/MAMP/tmp/php upload_max_filesize = 32M post_max_size = 48M 2/ 控制器 <?php //AcmeDemoBundle/Controller/DemoController namespace Acme\DemoBundle\Controller; use Symfony\Component\HttpFoundation\Request; class DemoController extends Controller { public function createAction(Request $request) {
  • Symfony 渲染控制器异常模板(“”)(Symfony render controller exception template (“”))
    问题 我试图在树枝中渲染一个控制器。 我遵循文档“嵌入其他控制器”。 我经常使用 render(controller()) 方法得到一个例外:在渲染模板 ("") 期间抛出了一个异常 在我的 template.html.twig 中 {{ render(controller('MyBundle:User:logo')) }} 在我的控制器中 class UserController extends Controller { public function logoAction() { return $this->render('MyBundle:User:logo.html.twig', []); } } 如果我在我的模板中使用它效果很好 {% render url('logo') %} 随着路线 logo: pattern: /logo defaults: { _controller: MyBundle:User:logo } 但我想使用 render(controller()) 我做错了什么? 编辑,添加有关异常的更多信息: An exception has been thrown during the rendering of a template ("") in MyBundle:My/Dashboard:dashboard.html.twig at line 51
  • Symfony2 - 在 TWIG 模板中获取当前 URL 或路由?(Symfony2 - Get the current URL or route in TWIG template?)
    问题 我的路线是 admin: path: /admin/ defaults: { _controller: CatalogWebBundle:Admin:admin } 如何在 PHP 模板中获取路由名称? 回答1 获取当前 URL $request->getRequestUri(); 或app.request.uri 至于路由本身,最佳做法是将其作为参数注入控制器,请参阅此处的文档。 您可以使用$request->attributes->get('_route')或app.request.attributes.get('_route')但它不是那么可靠,例如它不会像您转发到的转发一样使用控制器,而不是路径。 根据创建者 Fabien (@fabpot) 的说法,它实际上仅用于调试目的,因此我不会为了将来的升级而依赖它。 边注 记住要避免 $request->get() 任何时候都可以,所以没有$request->get('_route')正如我在类似问题的一些答案中看到的那样 如果您不需要控制器的灵活性,最好从适当的公共属性中显式获取请求参数(属性、查询、请求) 原因是它将查看所​​述公共属性(属性、查询和请求),而不仅仅是一个(属性),使其速度慢得多 回答2 直接在 Twig 中做不是一件好事,但您仍然可以这样做。 更好的方法是将它作为来自控制器的参数传递。 在 Twig
  • Unknown Entity namespace alias in symfony2
    Hey I have two bundles in my symfony2 project. one is Bundle and the other one is PatentBundle. My app/config/route.yml file is MunichInnovationGroupPatentBundle: resource: "@MunichInnovationGroupPatentBundle/Controller/" type: annotation prefix: / defaults: { _controller: "MunichInnovationGroupPatentBundle:Default:index" } MunichInnovationGroupBundle: resource: "@MunichInnovationGroupBundle/Controller/" type: annotation prefix: /v1 defaults: { _controller: "MunichInnovationGroupBundle:Patent:index" } login_check: pattern: /login_check logout: pattern: /logout inside my controller i have <?php
  • 使用 Symfony 框架在生产服务器上登录失败的用户(由于……无法处理身份验证请求)(Failed user login on production server using Symfony framework (Authentication request could not be processed due to…))
    问题 我正在将 Symfony 用于一个项目,并且过去 2 天我一直试图让登录名在生产服务器上工作但没有成功。 我不断收到错误 由于系统问题,无法处理身份验证请求。 我已经按照这里的指南 (http://symfony.com/doc/current/cookbook/security/entity_provider.html) 设置从数据库加载用户。 我的security.yml文件: security: encoders: Symfony\Component\Security\Core\User\User: plaintext Acceptme\UserBundle\Entity\User: plaintext role_hierarchy: ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] providers: in_memory: memory: users: patricia: password: patricia roles: 'ROLE_ADMIN' users: name: user_provider entity: { class: AcceptmeUserBundle:User, property: username } firewalls: user_area: pattern: ^/
  • 在 TWIG 模板中获取控制器名称(Get controller name in TWIG template)
    问题 我正在学习 symfony2.3,当我尝试在 twig 模板中获取控制器名称时出现错误。 控制器: namespace Acme\AdminBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; class DefaultController extends Controller { public function indexAction($name) { return $this->render('AcmeAdminBundle:Default:index.html.twig', array('name' => $name)); } } 在我的 TWIG 模板中: {% extends '::base.html.twig' %} {% block body %} {{ app.request.get('_template').get('controller') }} Hello {{ name }}!!! {% endblock %} 输出: Impossible to invoke a method ("get") on a NULL variable ("") in
  • 显示特定的路由而不是错误页面(404)-Symfony2(Show a specific route instead of error page (404) - Symfony2)
    问题 我只想显示/ sitemap页面,而不是错误页面/ 404。 当然,我不需要重定向,但我仍然希望设置404 HTTP响应标头。 这可能吗? 我所能看到的就是如何在Twig中设置模板。 我绝对不希望重定向。 回答1 如Symfony Cookbook所示,您可以通过两种方式覆盖错误页面: 覆盖模板使用自定义异常控制器 如果只想显示404( HttpNotFoundException )异常上的/sitemap路由,则可以通过在app/Resources/TwigBundle/views/Exception/error404.html.twig创建一个新模板来覆盖Twig异常模板。 食谱中未显示的另一种方法是使用事件侦听器。 当内核遇到异常时,将调度kernel.exception事件。 默认情况下,该异常被Twig提供的异常侦听捕获。 您可以创建自己的事件侦听器,以侦听kernel.exception事件并呈现页面: <?php use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpFoundation
  • 在 Twig / Symfony2 中引用模板(Referencing a template in Twig / Symfony2)
    问题 一个包中的 Twig 模板调用另一个包中的另一个 Twig 模板,但我无法引用它。 该文件在此文件夹中: src/Company/Bundle/ActivityBundle/Resources/views/Default/index.html.twig 我试图从另一个包中的 Twig 文件中引用它,如下所示: {% extends "Company/Bundle/ActivityBundle/index.html.twig" %} 我已经尝试了所有可能的组合,但它只是没有选择它,我收到了“Twig_Error_Loader:无法找到模板”错误 回答1 我认为您错过了模板路径的Default部分: {% extends "Company/Bundle/ActivityBundle/Default/index.html.twig" %} 或者我更喜欢: {% extends "CompanyBundleActivityBundle:Default:index.html.twig" %} 希望这可以帮助 ;)
  • 使用 ajax 将模板渲染到 Symfony2(Render template into Symfony2 with ajax)
    问题 我的控制器中有索引路由的操作 路由.yml index: pattern: /index defaults: { _controller:AcmeDemoBundle:Default:index } 此路径的控制器 public function indexAction() { return $this->render('AcmeDemoBundle:Plugin:index.html.twig'); } 和 index.html.twig 模板 {% extends'::base.html.twig' %} {% block stylesheets %} {% stylesheets filter='cssrewrite' output='css/*.css' 'bundles/acmedemo/css/*' %} <link href="{{ asset_url }}" type="text/css" rel="stylesheet" /> {% endstylesheets %} {% endblock stylesheets %} {% block body %} <br> <div class="container"> <div class="wp_attachment_holder"> <div class="imgedit-response" id=
  • Get controller name in TWIG template
    I am learning symfony2.3, and I am getting an error when I try to get controller name in twig template. Controller: namespace Acme\AdminBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; class DefaultController extends Controller { public function indexAction($name) { return $this->render('AcmeAdminBundle:Default:index.html.twig', array('name' => $name)); } } In my TWIG template: {% extends '::base.html.twig' %} {% block body %} {{ app.request.get('_template').get('controller') }} Hello {{ name }}!!! {% endblock %} Output
  • Symfony2: Twig: Default template file in custom location
    I try to load a simple base.html.twig template file that was moved from symfony's default location app/Resources/views/ to the custom location theme/. The template file contains: <!DOCTYPE html> <html> <head> ... </head> <body> {% block body %}{% endblock %} </body> </html> Extending the above template file by the controller Acme\Core\CoreBundle\Controller by using the controller-specific template {% extends '::base.html.twig' %} {% block body %} Hello world! {% endblock %} leads to an error saying Unable to find template "::base.html.twig" in "AcmeCoreCoreBundle:Default:index.html.twig".. How