收听事件:User::creating
Listen to Event : User::creating
我正在为 OctoberCms 开发一个新插件。我想限制某些特定域的前端注册。
我试过了:
class Plugin extends PluginBase
{
[......]
public function boot()
{
// Listen for user creation
Event::listen('eloquent.creating: October\Rain\Auth\Models\User', function($model) {
{
$this->checkDomains($user);
[.....]
}
}
}
但是我的监听器不工作。你知道什么是事件吗,我应该在创建新帐户之前收听 catch 吗?
谢谢
您可以像这样绑定到所有模型内部事件:
User::extend(function($model) {
$model->bindEvent('model.beforeSave', function() use ($model) {
// do something
});
});
您可以将 before
和 after
用于 create
、update
、save
、fetch
和 delete
或者,您可以使用,
public function boot()
{
User::creating(function($model) {
var_dump($model->name);
});
}
可收听的事件:
creating
、created
、updating
、updated
、deleting
、deleted
、saving
、saved
、restoring
, restored
您指的是 Front-End 用户的注册吗? - 我假设您使用的是 RainLab 用户插件,它在 Account component 中触发了一个事件 rainlab.user.beforeRegister
,或者您可以在模型的 beforeCreate()
事件
中添加一个自定义事件
然后只需在插件的根目录中创建一个 init.php
文件并在其中列出您的侦听器:
Event::listen('rainlab.user.beforeRegister', 'Path\To\ListenersClass');
您还可以使用以下内容:
\Event::listen('eloquent.creating: RainLab\User\Models\User', function($user){
$this->checkDomains($user);
});
我正在为 OctoberCms 开发一个新插件。我想限制某些特定域的前端注册。
我试过了:
class Plugin extends PluginBase
{
[......]
public function boot()
{
// Listen for user creation
Event::listen('eloquent.creating: October\Rain\Auth\Models\User', function($model) {
{
$this->checkDomains($user);
[.....]
}
}
}
但是我的监听器不工作。你知道什么是事件吗,我应该在创建新帐户之前收听 catch 吗?
谢谢
您可以像这样绑定到所有模型内部事件:
User::extend(function($model) {
$model->bindEvent('model.beforeSave', function() use ($model) {
// do something
});
});
您可以将 before
和 after
用于 create
、update
、save
、fetch
和 delete
或者,您可以使用,
public function boot()
{
User::creating(function($model) {
var_dump($model->name);
});
}
可收听的事件:
creating
、created
、updating
、updated
、deleting
、deleted
、saving
、saved
、restoring
, restored
您指的是 Front-End 用户的注册吗? - 我假设您使用的是 RainLab 用户插件,它在 Account component 中触发了一个事件 rainlab.user.beforeRegister
,或者您可以在模型的 beforeCreate()
事件
然后只需在插件的根目录中创建一个 init.php
文件并在其中列出您的侦听器:
Event::listen('rainlab.user.beforeRegister', 'Path\To\ListenersClass');
您还可以使用以下内容:
\Event::listen('eloquent.creating: RainLab\User\Models\User', function($user){
$this->checkDomains($user);
});