简体   繁体   中英

Override FosUserBundle template in own bundle, not app/Resources

According to this 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.

Is there a way to place these templates into my own bundle directory instead?

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

No, the template names are hardcoded inside the FOSUserBundle's controllers, thus you are not able to place them in your own bundle. (Unless you overwrite all controllers completely)

No, you can't. Template names are hardcoded in fosub controllers. 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. Please try below code that may help you

config.yml

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 %}

You may look at your versions of FOSUser and Symfony and read this thread : 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. 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

I had the same issue today. My config.yml file is below:

# 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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