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("myemail@example.com")
->from("myemail@example.com", "My App")
->subject('New User')
->body("new user")
->html(true)
->send();
});
How does the listener know an event fired?
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.phpat 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.phpand require this file insideapplication/start.phpfor better readability.Hope this helps!