Override bundle template from another bundle in Symfony 4/5

前端 未结 3 1965
轻奢々
轻奢々 2021-02-09 07:03

because bundle inheritance is deprecated since Symfony 3.4 and will be removed in 4.0, I\'m finding a new solution. I have:

  • Application
  • FooBundle
3条回答
  •  抹茶落季
    2021-02-09 07:47

    I will show how it looks in my case(Symfony 5.1). To overwrite views for bundles JMoseCommandSchedulerBudnle and TwigBundle:

    1. Create your own directory with custom third party bundle templates(I put them In Resources/views/bundles):

    2. Create folders for each bundle where you want to override templates:

    3. Create custom templates(for example to create custom Twig Error Pages):

    4. Implement PrependExtensionInterface and add custom paths to the Twig Extension configuration:

    class AppngCmsExtension extends Extension implements PrependExtensionInterface {
        public function load(array $configs, ContainerBuilder $container)
        {
            $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
            $loader->load('services.yaml');
        }
    
        public function prepend(ContainerBuilder $container)
        {
            // I recommend using FileLocator here 
            $thirdPartyBundlesViewFileLocator = (new FileLocator(__DIR__ . '/../Resources/views/bundles'));
    
            $container->loadFromExtension('twig', [
                'paths' => [
                    $thirdPartyBundlesViewFileLocator->locate('JMoseCommandSchedulerBundle') => 'JMoseCommandScheduler',
                    $thirdPartyBundlesViewFileLocator->locate('TwigBundle') => 'Twig',
                ],
            ]);
        }
    }
    

提交回复
热议问题