Listen to Event : User::creating

I'm doing a new plugin for OctoberCms. I would like to limit the front end registration for some specific domains.

I tried to this :

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);
            [.....]
        }
    }
}

But my listener is not working. Do you know what is the Event, I should listen to catch before a new account is created ?

Thanks

Upvotes: 2

Views: 3182

Answers (4)

Meysam
Meysam

Reputation: 18137

You can also use the following:

\Event::listen('eloquent.creating: RainLab\User\Models\User', function($user){
    $this->checkDomains($user);
});

Upvotes: 2

Raja Khoury
Raja Khoury

Reputation: 3195

Are you referring to a Front-End User's registration ? - I assumed you're using the RainLab User Plugin which has an event rainlab.user.beforeRegister fired in the Account component or you can add a custom one in the model's beforeCreate() event

then simply create a init.php file in the root directory of your plugin and list your listeners there :

Event::listen('rainlab.user.beforeRegister', 'Path\To\ListenersClass');

Upvotes: 1

Surahman
Surahman

Reputation: 1080

Alternatively, you can use,

public function boot()
{
    User::creating(function($model) {
        var_dump($model->name);
    });
}

available events to listen: creating, created, updating, updated, deleting, deleted, saving, saved, restoring, restored

Upvotes: 2

dragontree
dragontree

Reputation: 1799

You can bind to all of the model internal events like this:

User::extend(function($model) {
    $model->bindEvent('model.beforeSave', function() use ($model) {
        // do something
    });
});

You can use before and after for create, update, save, fetch and delete

Upvotes: 4

Related Questions