because bundle inheritance is deprecated since Symfony 3.4 and will be removed in 4.0, I'm finding a new solution. I have:
And I haven't problem with overriding templates in Application. But I need to override templates from BarBundle in FooBundle. It was so easy with bundle inheritance but I'm lost now :)
I tried twig namespaces - no success, but I configured it in wrong way maybe. My goal is to have base templates in BarBundle that I can override in FooBundle or Application or both. (it is because BarBundle is third-party bundle and FooBundle is my bundle used in many projects).
Is it still possible without bundle inheritance and how?
Thanks.
The easiest way to "override" a bundle's routing is to never import it at all. Instead of importing a third-party bundle's routing, copy that routing file into your application, modify it, and import it instead.
A Symfony bundle is a collection of files and folders organized in a specific structure. The bundles are modeled in such a way that it can be reused in multiple applications. The main application itself is packaged as a bundle and it is generally called AppBundle.
So I have recently needed same functionality and with help from comment @NicoHasse I managed to make working example
In your bundle bundle extension class you need to implement PrependExtensionInterface
and then you can modify twig paths. Then you need to know original namespace your you need to override (php bin/console debug:twig
).
You can confirm its working with twig debug command where you should see your path at first place of that namespace.
class YourExtensionClass extends Extension implements PrependExtensionInterface
{
public function prepend(ContainerBuilder $container)
{
$container->loadFromExtension('twig', [
'paths' => [
'%kernel.project_dir%/vendor/xx/yy/zzz' => 'OriginalVNamespace',
]
]);
Here's a more comprehensible example
Let's start with making some assumptions
Your bundle's name: AcmeBundle
Bundle you want to override: FOSUserBundle
Run command php bin/console debug:twig
and find the namespace of the bundle you want to override. In this case it's @FOSUser
.
Your bundle extension should look like this
<?php // src/AcmeBundle/DependencyInjection/AcmeExtension.php
namespace AcmeBundle\DependencyInjection;
// ...
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
class AcmeExtension extends Extension implements PrependExtensionInterface
{
// ...
public function prepend(ContainerBuilder $container)
{
$container->loadFromExtension('twig', array(
'paths' => array(
'%kernel.project_dir%/src/AcmeBundle/Resources/FOSUserBundle/views' => 'FOSUser', // You use the namespace you found earlier here. Discard the `@` symbol.
),
));
}
}
Now you can create src/AcmeBundle/Resources/FOSUserBundle/views/Security/login.html.twig
to override the login template of FOSUserBundle.
This was just an example for FOSUserBundle. You can change bundle names depending on what you're trying to override.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With