【问题标题】:OctoberCMS: How to make the User model notifiable?OctoberCMS:如何使用户模型可通知?
【发布时间】:2018-03-01 21:01:38
【问题描述】:

RainLab\User\Models\User 类不使用 Notifiable 特征,因此无法在其上调用 notifyNotification::send。我想编写一个插件来扩展 RainLab\User\Models\User 并向其添加 Notifiable 特征。我该怎么做?

【问题讨论】:

    标签: php laravel octobercms laravel-notification


    【解决方案1】:

    我已经将 trait 实现为一种行为:https://github.com/CptMeatball/notifiable-user

    它是如何工作的?

    此插件充当 Notifiable trait 的简单包装器,并将其作为行为添加到 User 模型中。它通过在行为类中插入特征来工作。然后在插件的引导方法期间将其添加到用户模型中。就这么简单。

    NotifiableBehavior

    use Illuminate\Notifications\Notifiable as NotifiableTrait;
    class Notifiable extends \October\Rain\Database\ModelBehavior
    {
        use NotifiableTrait;
        public function __call($name, $params = null)
        {
            if (!method_exists($this, $name) || !is_callable($this, $name)) {
                return call_user_func_array([$this->model, $name], $params);
            }
        }
    }
    

    插件.php

    public function boot()
    {
        User::extend(function($model) {
            $model->implement[] = 'CptMeatball.NotifiableUser.Behaviors.Notifiable';
        });
    }
    

    您可以对任何其他特征使用相同的原则。

    【讨论】:

    • 在此处而不是在 github 存储库中发布最少量的代码。事后看来,github 存储库通常会被删除,从而导致答案无效。
    • 更新了答案@AdnanY。感谢您的提醒。
    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2019-05-16
    相关资源
    最近更新 更多