简体   繁体   English

在自己的捆绑软件(而非应用程序/资源)中覆盖FosUserBundle模板

[英]Override FosUserBundle template in own bundle, not app/Resources

According to this http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_templates.html 根据此http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_templates.html

I can override FOSUserBundle templates in the app/Resources directory or create a child bundle. 我可以在app / Resources目录中覆盖FOSUserBundle模板或创建一个子捆绑包。

Is there a way to place these templates into my own bundle directory instead? 有没有办法将这些模板放入我自己的bundle目录中?

I've tried adding: 我尝试添加:

twig:
   debug:            "%kernel.debug%"
   strict_variables: "%kernel.debug%"
   paths:
       '%kernel.root_dir%/../src/AppBundle/Resources/FOSUserBundle/views/': FOSUSerBundle

to my config.yml 到我的config.yml

No, the template names are hardcoded inside the FOSUserBundle's controllers, thus you are not able to place them in your own bundle. 不,模板名称在FOSUserBundle的控制器内是硬编码的,因此您不能将它们放在自己的捆绑软件中。 (Unless you overwrite all controllers completely) (除非您完全覆盖所有控制器)

No, you can't. 不,你不能。 Template names are hardcoded in fosub controllers. 模板名称在fosub控制器中进行了硬编码。 But, as a workaround you can include your own template in rewrited one. 但是,作为一种变通办法,您可以在重写的模板中包含自己的模板。 For sample, do it like this: 对于示例,请执行以下操作:

// app/Resources/FOSUserBundle/views/layout.html.twig
{% include '@YourBundleName/layout.html.twig' %}

I did template overriding for FosUserBundle in one of my project developed in symfony2.3 version. 我在以symfony2.3版本开发的项目之一中为FosUserBundle进行了模板覆盖。 Please try below code that may help you 请尝试以下可能对您有帮助的代码

config.yml 配置文件

twig:
   debug:            "%kernel.debug%"
   strict_variables: "%kernel.debug%"

FrontBundle/Controller/LoginController.php FrontBundle / Controller / LoginController.php

<?php
namespace Dhi\UserBundle\Controller;

use FOS\UserBundle\Controller\SecurityController as Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\RedirectResponse;

class LoginController extends Controller
{
    protected function loginController(array $data)
    {
        $request = $this->get('request');
        return $this->render('FrontBundle:login:login.html.twig');
    }
}

FrontBundle/Resources/views/login/login.html.twig FrontBundle / Resources / views / login / login.html.twig

{% extends "DhiUserBundle::layout.html.twig" %}
{% trans_default_domain 'FOSUserBundle' %}
{% block body %}
    {% block fos_user_content %}

        {% trans_default_domain 'FOSUserBundle' %}

        // Your login form code here

    {% endblock fos_user_content %}
{% endblock body %}

You may look at your versions of FOSUser and Symfony and read this thread : https://github.com/FriendsOfSymfony/FOSUserBundle/issues/2439 您可以查看您的FOSUser和Symfony版本并阅读以下线程: https : //github.com/FriendsOfSymfony/FOSUserBundle/issues/2439

I've experience this bug which didn't allow me to override FOS templates because they used (in latest version 2.0.0-beta) the namespaced syntax (the one with @ symbol) to link templates. 我遇到了这个错误,该错误不允许我覆盖FOS模板,因为它们使用(在最新版本2.0.0-beta中)命名空间语法(带有@符号的语法)链接模板。 And because of it, before Symfony's 2.7.23 version, you can't override templates by putting the right named file in the same folders as described here : http://symfony.com/doc/master/bundles/FOSUserBundle/overriding_templates.html 因此,在Symfony的2.7.23版本之前,您无法通过将正确的命名文件放在此处描述的相同文件夹中来覆盖模板: http : //symfony.com/doc/master/bundles/FOSUserBundle/overriding_templates。 html

I had the same issue today. 我今天有同样的问题。 My config.yml file is below: 我的config.yml文件如下:

# Twig Configuration
twig:
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    paths:
        "%kernel.root_dir%/../src/WebBundle/Resources/views/user/": FOSUser

I added all FOSUserBundle templates under WebBundle/Resources/views/user/ and made some extend/include changes in all templates and now it works. 我在WebBundle / Resources / views / user /下添加了所有FOSUserBundle模板,并对所有模板进行了一些扩展/包含更改,现在可以使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM