Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Laravel events?

Tags:

php

laravel

I want to be notified when a new user signs up. In my controller I fire an event as follows.

Event::fire('myapp.new_user', array($this->data['user']->email));

Where do I define the listener?

Event::listen('myapp.new_user', function ($uid) {
    Message::to("[email protected]")
                ->from("[email protected]", "My App")
                ->subject('New User')
                ->body("new user")
                ->html(true)
                ->send();
});

How does the listener know an event fired?

like image 377
sdot257 Avatar asked Nov 13 '12 23:11

sdot257


People also ask

Where are events and listeners stored in Laravel?

All the event classes in Laravel are stored in the app/Events folder and the listeners are stored in the app/Listeners folder. The artisan command for generating events and listeners in your web application is shown below − This command generates the events and listeners to the respective folders as discussed above.

What are Laravel model events?

Laravel Model events allow you to tap into various points in a model’s lifecycle, and can even prevent a save or delete from happening. The Laravel model events documentation outlines how you can hook into these events with event classes, but this article aims to build upon and fill in a few additional details on setting up events and listeners.

How does event discovery work in Laravel?

When event discovery is enabled, Laravel will automatically find and register your events and listeners by scanning your application's Listeners directory. In addition, any explicitly defined events listed in the EventServiceProvider will still be registered.

How to add userregistered event in Laravel?

Laravel provides EventServiceProvider file where you need to register your events and listeners. In our case we need to add an event and listener for user registration. So, open the file EventServiceProvider.php and add UserRegistered event in it.


3 Answers

You need to make sure your listeners are defined prior to your application logic executing, when events are thrown they can only be caught by already registered listeners, it does not look for new ones.

On small projects I just place my listeners in application/start.php at the bottom of the file. This file happens before your routes are ran and it serves as sort of a application config file with some logic to it. You need to place these events towards the bottom of the file, at least after the Autoloader mappings have been registered.

On Larger projects I will create application/listeners.php and require this file inside application/start.php for better readability.

Hope this helps!

like image 167
William Cahill-Manley Avatar answered Nov 01 '22 23:11

William Cahill-Manley


You can also define classes to handle specific events and then use a Service Provider to register them.

Below is a basic example:

app/NewUserListener.php

The listener which is called when the event is fired:

class NewUserListener
{
    public function handle($uid)
    {
        // Send an email here
    }
}

app/ListenerServiceProvider.php

The ServiceProvider - remember and add this to the list of Service Providers in L4 Config.

use Illuminate\Support\ServiceProvider;

class ListenerServiceProvider extends ServiceProvider
{
        public function register()
        {
            Event::listen('myapp.new_user', 'NewUserListener');
            // Register more events here...
        }
}

If you organize listeners etc. into appropriately named folders, it ends up being a lot easier to maintain should you have a bunch of listeners later. You can also instantiate and test the listeners if you write them in this manner.

like image 45
Darren Taylor Avatar answered Nov 01 '22 23:11

Darren Taylor


try :

Event::listen('myapp.new_user', function ($uid) {
Message::to("[email protected]")
            ->from("[email protected]", "My App")
            ->subject('New User')
            ->body("new user")
            ->html(true)
            ->send();
return 'test my event';
});

http://laravel.com/docs/events

like image 26
Ihab Shoully Avatar answered Nov 01 '22 23:11

Ihab Shoully