返回介绍

Extensibility

发布于 2024-06-22 20:04:58 字数 4196 浏览 0 评论 0 收藏 0

In some cases, you might want other extensions to extend your extension.

Backend

Extensions extend Flarum Core's backend via two mechanisms:

Unsurprisingly, you can make your extension extensible via the same mechanisms.

Custom Events

To learn about dispatching events and defining new ones, see the relevant documentation.

Custom Extenders

Lets say you've developed an extension that adds an alternative search driver to Flarum, but you want to allow other extensions to add support for custom filters / sorts. A custom extender could be a good way to accomplish this.

The implementation of extenders is actually quite simple. There are 3 main steps:

  1. Various methods (and the constructor) allow client code to specify options. For example:
  • Which model / API controller / validator should be extended?
  • What modifications should be made?
  1. An extend method takes the input from step 1, and applies it by modifying various container bindings and global static variables to achieve the desired effect. This is the "implementation" of the composer. The extend methods for all enabled extensions are run as part of Flarum's boot process.
  2. Optionally, extenders implementing Flarum\Extend\LifecycleInterface can have onEnable and onDisable methods, which are run when extensions that use the extender are enabled/disabled, and are useful for tasks like clearing various caches.

Accordingly, to create a custom extender, all you need to do is:

  1. Define a class that implements Flarum\Extend\ExtenderInterface.
  2. Accept arguments in the constructor, and various methods. Those methods should represent concrete "modifications".
  3. Implement an extend method that modifies your extension (or Flarum), typically via extending/modifying container bindings.
  4. Optionally, implement Flarum\Extend\LifecycleInterface if cleanup is needed on enable/disable.

Before designing your own extenders, we HIGHLY recommend looking through the implementations of core's extenders.

提示

Custom extenders introduced by your extension should be considered public API. You can add automated tests for them via our backend testing package.

警告

Custom extenders should NOT be used to run arbitrary logic during the Flarum boot process. That's a job for Service Providers. An easy way to check: if you're using extenders that you have defined in your own extension, you're probably doing something wrong.

Frontend

If you want other extensions to be able to use classes or functions defined in your extension (whether to reuse or modify via the extend/override utils), you'll need to export them in your extension's index.js (typically the same place where your extension's initializer is located).

For example:

app.initializers.add('your-extension-id', () => {
  // Your Extension Code Here
})

export {
  // Put all the stuff you want to export here.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文