跳到主要内容
版本:2.x

扩展其他扩展

Flarum 扩展不仅仅是为核心添加功能:扩展可以扩展其他扩展!

提示

To learn how to make your extension extensible, see the relevant documentation

依赖​

如果你的扩展依赖于另一个扩展,你将会确保:

  • 在您开始之前安装并启用了其他扩展。
  • 在您的扩展启用时不能禁用其他扩展。
  • 另一个扩展在您的扩展之前启动。

Flarum makes this very easy: just add the other extension to your extension's composer.json's require section.

For example, if you were building a new theme for the Flarum Tags extension, your composer.json would look like this:

{
// ...
"require": {
"flarum/core": "^2.0", // Since all extensions need to require core.
"flarum/tags": "*" // This tells Flarum to treat tags as a dependency of your extension.
},
// ...
}

可选依赖​

有时候,扩展A可能只在启用扩展B时才想扩展扩展B。 Sometimes, extension A might want to extend extension B only if extension B is enabled. In this case, we call B an "Optional Dependency" of A. For instance, a drafts extension might want to add support for saving private discussion drafts, but only if the private discussion extension is enabled.

这里的第一步是检测扩展B是否启用。 The first step here is detecting whether extension B is enabled. 就像这样:

if ('some-extension-id' in flarum.extensions) {
// do something
}

In the backend, you'll need to inject an instance of Flarum\Extension\ExtensionManager, and use its isEnabled() method. 就像这样:

<?php

use Flarum\Extension\ExtensionManager;

class SomeClass {
public function __construct(ExtensionManager $extensions)
{
$this->extensions = $extensions;
}

public function someMethod()
{
if ($this->extensions->isEnabled('some-extension-id')) {
// do something.
}
}
}

通常,如果你的扩展有可选依赖项,你会希望它在上述可选依赖项之后启动。 You can also do this by specifying composer package names (NOT flarum extension IDs) in an array for the extra.flarum-extension.optional-dependencies key of your composer.json.

就像这样:

{
// ...
"extra": {
"flarum-extension": {
"optional-dependencies": [
"flarum/tags"
]
}
},
// ...
}

从扩展导入​

In the backend, you can import the classes you need via regular PHP use statements:

<?php

use Flarum\Tags\Tag;

class SomeClass
{
public function someMethod()
{
return new Tag(); // This is not the correct way to instantiate models, it's just here for example of importing.
}
}

Note that if you're importing from an optional dependency which might not be installed, you'll need to check that the class in question exists via the class_exists function.

In the frontend, you can import any modules exported by other extensions via the ext:vendor/extension/.../module syntax. 就像这样:

从 'ext:flarum/tags/common/models/Tag'导入标签;