【问题标题】:why is my Event not working in octobercms?为什么我的活动在 octobercms 中不起作用?
【发布时间】:2019-04-22 10:36:24
【问题描述】:

我正试图围绕 octobercms 组和许可。我在权限和角色上添加了一个很棒的plugin,它直接且易于在页面/用户上实现,但我的问题是我是否必须像一个一个地从后端手动将所有用户添加到组中?

我创建了一个新插件,在 Plugin.php 文件中有这个, use Event; //on the top of the file

我在它的启动方法中有这个,可以在注册时使用代码seller 将用户添加到组中。

    public function boot()
{

Event::listen('rainlab.user.register', function($user){
    $group = 
    \Rainlab\User\Models\UserGroup::where('code', 'seller')->first();

    $user->groups()->add($group);
    $user->save();
});

似乎不起作用。我在所有这些中缺少什么?

plugin.php 文件

<?php namespace Corymillz\Newplug;

use System\Classes\PluginBase;
use Event;

class Plugin extends PluginBase
{
   public function registerComponents()
    {
    }

    public function registerSettings()
    {
    }

   public function boot()
{ 

Event::listen('rainlab.user.register', function($user,$postdata){
    $group = \Rainlab\User\Models\UserGroup::where('code', 'seller')->first();

    $user->groups = $group->id;
    $user->save();
});


}

}

【问题讨论】:

  • 你检查我的答案了吗?
  • 是的,我刚刚做到了。插件不再消失,但仍然没有将用户添加到组中
  • 事件rainlab.user.register 仅从前端触发。我希望你是从前端使用它
  • 是的,我是。我创建了一个新插件,代码在 plugin.php 文件中
  • 请完整粘贴您的plugin.php文件的启动方法。

标签: php laravel octobercms


【解决方案1】:

只需像这样更改您的代码。

Event::listen('rainlab.user.register', function($user,$postdata){
    $group = \Rainlab\User\Models\UserGroup::where('code', 'seller')->first();

    $user->groups = $group->id;
    $user->save();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 2016-06-30
    • 1970-01-01
    • 2016-08-01
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多