返回介绍

Settings

发布于 2021-11-04 22:44:46 字数 3954 浏览 973 评论 0 收藏 0

At some point while making an extension, you might want to read some of the forum's settings or store certain settings specific to your extension. Thankfully, Flarum makes this very easy.

Reading or changing settings can be done using an implementation of the SettingsRepositoryInterface. Because Flarum uses Laravel's service container (or IoC container) for dependency injection, you don't need to worry about where to obtain such a repository, or how to instantiate one. Instead, you can rely on the container to instantiate your class and inject the correct dependencies.


<?php


namespace acme\HelloWorld\ExampleDir;


use Flarum\Settings\SettingsRepositoryInterface;


class ClassInterfacesWithSettings
{
    /**
     * @var SettingsRepositoryInterface
     */
    protected $settings;


    public function __construct(SettingsRepositoryInterface $settings)
    {
        $this->settings = $settings;
    }
}

Great! Now the SettingsRepositoryInterface is available through $this->settings to our class.

To read settings, all we have to do is use the repository's get() function:

$this->settings->get('forum_title')

The get() function accepts two arguments:

  1. The name of the setting you are trying to read.
  2. (Optional) A default value if no value has been stored for such a setting. By default, this will be null.

Storing settings ist just as easy, use the set() function:

$this->settings->set('forum_title', 'Super Awesome Forum')

The set function also accepts two arguments:

  1. The name of the setting you are trying to change.
  2. The value you want to store for this setting.

The all() function returns an array of all known settings.

The delete($name) function lets you remove a named setting.

To learn more about adding settings through the admin dashboard, see the relevant documentation.

All settings are available in the admin frontend via the app.data.settings global. However, this is not done in the forum frontend, as anyone can access it, and you wouldn't want to leak all your settings! (Seriously, that could be a very problematic data breach).

Instead, if we want to use settings in the forum frontend, we'll need to serialize them and send them alongside the initial forum data payload.

This can be done via the Settings extender. For example:

extend.php


use Flarum\Extend;


return [
   (new Extend\Settings)
      ->serializeToForum('myCoolSetting', 'my.cool.setting.key')
      ->serializeToForum('myCoolSettingModified', 'my.cool.setting.key', function ($retrievedValue) {
        // This third argument is optional, and allows us to pass the retrieved setting through some custom logic.
        // In this example, we'll append a string to it.


        return "My Cool Setting: $retrievedValue";
      }, "default value!"),
]

Now, the my.cool.setting.key setting will be accessible in the frontend as app.forum.attribute("myCoolSetting"), and our modified value will be accessible via app.forum.attribute("myCoolSettingModified").

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

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

发布评论

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