[英]Override FosUserBundle template in own bundle, not app/Resources
根據此http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_templates.html
我可以在app / Resources目錄中覆蓋FOSUserBundle模板或創建一個子捆綁包。
有沒有辦法將這些模板放入我自己的bundle目錄中?
我嘗試添加:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
paths:
'%kernel.root_dir%/../src/AppBundle/Resources/FOSUserBundle/views/': FOSUSerBundle
到我的config.yml
不,模板名稱在FOSUserBundle的控制器內是硬編碼的,因此您不能將它們放在自己的捆綁軟件中。 (除非您完全覆蓋所有控制器)
不,你不能。 模板名稱在fosub控制器中進行了硬編碼。 但是,作為一種變通辦法,您可以在重寫的模板中包含自己的模板。 對於示例,請執行以下操作:
// app/Resources/FOSUserBundle/views/layout.html.twig
{% include '@YourBundleName/layout.html.twig' %}
我在以symfony2.3
版本開發的項目之一中為FosUserBundle
進行了模板覆蓋。 請嘗試以下可能對您有幫助的代碼
配置文件
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
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
{% 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 %}
您可以查看您的FOSUser和Symfony版本並閱讀以下線程: https : //github.com/FriendsOfSymfony/FOSUserBundle/issues/2439
我遇到了這個錯誤,該錯誤不允許我覆蓋FOS模板,因為它們使用(在最新版本2.0.0-beta中)命名空間語法(帶有@符號的語法)鏈接模板。 因此,在Symfony的2.7.23版本之前,您無法通過將正確的命名文件放在此處描述的相同文件夾中來覆蓋模板: http : //symfony.com/doc/master/bundles/FOSUserBundle/overriding_templates。 html
我今天有同樣的問題。 我的config.yml文件如下:
# Twig Configuration
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
paths:
"%kernel.root_dir%/../src/WebBundle/Resources/views/user/": FOSUser
我在WebBundle / Resources / views / user /下添加了所有FOSUserBundle模板,並對所有模板進行了一些擴展/包含更改,現在可以使用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.