从其他捆绑包中覆盖您自己的 Symfony 捆绑包中的控制器、视图和资源 [英] Override Controller, Views and Resources in your own Symfony Bundles from other Bundles
问题描述
如何覆盖您自己的供应商/捆绑包控制器或从另一个捆绑包查看?
Symfony 有一种默认方式来覆盖 第三方 vendor
捆绑包,方法是在您自己的 app
或 src
中添加文件> 目录,例如app/Resources/AcmeBlogBundle/views/Blog/index.html.twig
或 src/Acme/BlogBundle/Resources/views/Blog/index.html.twig
.>
- 引用父"不起作用,因为这是为 OneToOne 包覆盖设计的.
目前我知道的唯一方法是告诉 Symfony 在同一命名空间中查找替代文件,例如在我的主控制器中,我将名称空间
Gutensite
注册到其他位置.$loader = $GLOBALS['loader'];//用户当前模板的路径$loader->add('Gutensite', $template->getPath().'/src', true);//用户自定义客户端文件的路径$loader->add('Gutensite', \Gutensite\PATH_CLIENT.'/src', true);
然后我会将我的自定义控制器放在模板中这样的目录中:
Templates\LunarBundle\src\Gutensite\CmsBundle\Controllers\DashController.php
这行得通,但我想就潜在问题或更好的解决方案提供反馈.
解决方案也许这适合你:https://github.com/liip/LiipThemeBundle
<块引用>此捆绑包为您提供了为每个捆绑包添加主题的可能性.在您的包目录中,它会在 Resources/themes/下查找,如果没有找到匹配的文件,它将返回到正常的 Resources/views.
特别看主题级联顺序配置
How can you override your own vendor/bundle controller or view from another bundle?
Symfony has a default way of overriding third party
vendor
bundles by adding files in your ownapp
orsrc
directory, e.g.app/Resources/AcmeBlogBundle/views/Blog/index.html.twig
orsrc/Acme/BlogBundle/Resources/views/Blog/index.html.twig
.- Override Any Part of a Symfony Bundle
- How to Use Bundle Inheritance to Override Parts of a Bundle
- Overriding Bundle Templates
But I have my own custom core vendor bundles in the
src
directory (e.g.src/Gutensite/CmsBundle
) and I need to override controllers, views (twig) and resources (images, css) in template specific bundles that should take precedence whenever they need to alter the default behavior of the platform for a specific design (e.g.src/Templates/LunarBundle/
).Views and Resources
So to overwrite the CmsBundle view in my Lunar template, I may put files here:
Templates/LunarBundle/Resources/GutensiteCmsBundle/views/dash.html.twig Templates/LunarBundle/Resources/GutensiteCmsBundle/public/css/dash.css Templates/LunarBundle/Resources/GutensiteCmsBundle/public/images/icon.png
If I make my own
dash.html.twig
template I could reference the files locally, e.g.{% stylesheets '@TemplatesAdminBundle/Resources/GutensiteCmsBundle/public/css/dash.css' %} <link rel="stylesheet" href="{{ asset_url }}"> {% endstylesheets %}
But if I only added a custom image or css, the original
Gutensite\CmsBundle\Resources\views\dash.html.twig
template would find the custom css in myTemplatesLunarBundle
when it was referenced as:{% stylesheets '@GutensiteCmsBundle/Resources/public/css/dash.css' %} <link rel="stylesheet" href="{{ asset_url }}"> {% endstylesheets %}
But Symfony default behavior doesn't allow you to override your own bundles from your own bundles. So how do you do this?
Controllers
I need the same overriding capabilities for controllers. One template bundle (e.g.
Templates\LunarBundle
may need to overwrite a lot of different core vendor bundles (e.g.Gutensite\CmsBundle\Controllers\DashController.php
andGutensite\ArticleBundle\Controllers\ArticleController.php
). So this method to reference the "parent" wouldn't work, since that's designed for OneToOne bundle overriding.Currently the only method I know of is to tell Symfony to find alternative files in the same namespace, e.g. in my primary controller I register the namespace
Gutensite
to alternative locations.$loader = $GLOBALS['loader']; // path to the user's current template $loader->add('Gutensite', $template->getPath().'/src', true); // path to the user's custom client files $loader->add('Gutensite', \Gutensite\PATH_CLIENT.'/src', true);
And then I would put my custom controllers in a directory like this in my template:
Templates\LunarBundle\src\Gutensite\CmsBundle\Controllers\DashController.php
This works, but I'd like feedback on potential problems or better solutions.
解决方案Maybe this is something for you: https://github.com/liip/LiipThemeBundle
This bundle provides you the possibility to add themes to each bundle. In your bundle directory it will look under Resources/themes/ or fall back to the normal Resources/views if no matching file was found.
Especially see the configuration for Theme Cascading Order
这篇关于从其他捆绑包中覆盖您自己的 Symfony 捆绑包中的控制器、视图和资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!