Symfony2 - bundle之外的东西



我目前正在从事我需要比捆绑更需要的东西的项目。我称之为"模块"的东西。它应该与捆绑包不同,因为当项目启动时,系统不知道将使用哪些"模块"以及如何配置它们。

此外,我将使用这些类似于捆绑包的模块

$response = $this->forward('AcmeHelloModule:Hello:fancy');

$response = $this->forward('Acme/Hello:Hello:fancy');

这里 HelloController->fancyAction(); 将被执行。这个控制器在文件/src/modules/Acme/Hello/Controller/HelloController中描述.php

所以问题是如何实现这一点?

一个解决方案是实现一个可以动态安装,加载和运行所谓的模块的PluginBundle。

PluginBundle 根本不包含特定的插件代码,而是包含模块/插件的运行时环境。 然后,您可以将启用了哪些插件/模块保存在数据库中,并在运行时动态加载它们。

通过这种解决方案,应该可以像WordPress中一样创建一个动态插件机制。 在运行时修改 AppKernel 不是一个好的解决方案,因为在解除捆绑包时还必须清除缓存。

在 AppKernel 中添加以下方法:

public function getBundle($name, $first = true)
{
    if (substr($name, -6) == 'Module')) {
        return $this->getBundle('ModuleBundle')->getModule($name, $first);
    }
    return parent::getBundle($name, $first);
}

并且所有逻辑都在模块捆绑包中运行。但请确保响应类型与内核->getBundle() 相同;

最新更新