首页 文章

从其他Bundle覆盖您自己的Symfony Bundle中的Controller,Views和Resources

提问于
浏览
3

How can you override your own vendor/bundle controller or view from another bundle?

Symfony有一种默认方法可以通过在您自己的 appsrc 目录中添加文件来覆盖 third party vendor 包,例如: app/Resources/AcmeBlogBundle/views/Blog/index.html.twigsrc/Acme/BlogBundle/Resources/views/Blog/index.html.twig .

但是我在 src 目录中有我自己的自定义核心供应商包(例如 src/Gutensite/CmsBundle ),我需要在模板特定的包中覆盖控制器,视图(树枝)和资源(图像,css),这些包应该在需要更改默认值时优先使用特定设计的平台行为(例如 src/Templates/LunarBundle/ ) .

视图和资源

Lunar Dashboard

因此,为了覆盖我的Lunar模板中的CmsBundle视图,我可以将文件放在这里:

Templates/LunarBundle/Resources/GutensiteCmsBundle/views/dash.html.twig
Templates/LunarBundle/Resources/GutensiteCmsBundle/public/css/dash.css
Templates/LunarBundle/Resources/GutensiteCmsBundle/public/images/icon.png

如果我制作自己的 dash.html.twig 模板,我可以在本地引用这些文件,例如

{% stylesheets '@TemplatesAdminBundle/Resources/GutensiteCmsBundle/public/css/dash.css' %}
<link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}

但是,如果我只添加了自定义图像或css,原始 Gutensite\CmsBundle\Resources\views\dash.html.twig 模板会在我的 TemplatesLunarBundle 中找到自定义css,当它被引用为:

{% 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?

控制器

我需要与控制器相同的覆盖功能 . 一个模板包(例如 Templates\LunarBundle 可能需要覆盖许多不同的核心供应商包(例如 Gutensite\CmsBundle\Controllers\DashController.phpGutensite\ArticleBundle\Controllers\ArticleController.php ) . 所以this method引用为OneToOne包覆盖设计的"parent" wouldn 't work, since that' .

目前我所知道的唯一方法是告诉Symfony在同一名称空间中查找替代文件,例如在我的主控制器中,我将命名空间 Gutensite 注册到其他位置 .

$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);

然后我会将我的自定义控制器放在我的模板中这样的目录中:

Templates\LunarBundle\src\Gutensite\CmsBundle\Controllers\DashController.php

This works, but I'd like feedback on potential problems or better solutions.

1 回答

相关问题