Laravel 5 event not working -
sr, try using laravel 5 example below, not working.
namespace app\events; use illuminate\queue\serializesmodels; class didsomething extends event { use serializesmodels; public function __construct() { return; } }
and handles
namespace app\handlers\events;
use app\events\didsomething;
class dosomething { public function __construct() { } public function handle(didsomething $event) { dd($event); } }
and eventserviceprovider.php
namespace app\providers; use illuminate\contracts\events\dispatcher dispatchercontract; use illuminate\foundation\support\providers\eventserviceprovider serviceprovider; class eventserviceprovider extends serviceprovider { /** * event handler mappings application. * * @var array */ protected $listen = [ app\events\didsomething::class=>[ app\handlers\events\dosomething::class ] ]; /** * register other events application. * * @param \illuminate\contracts\events\dispatcher $events * @return void */ public function boot(dispatchercontract $events) { parent::boot($events); // }
}
in controller, used \event::fire(new didsomething());
and had tried command line
php artisan event:generate php artisan optimize composer update
generate event
art make:event logme
generate event handler event
art handler:event logme --event="logme"
make sure register event , it’s hander in event service
provider (app/providers/eventserviceprovider.php)
//
protected $listen = [ 'event.name' => [ 'eventlistener', ], \xxx\events\logme::class => [ \xxx\handlers\events\logme::class, ], ];
- fire event
event::fire(new logme());
-or- can use event helperevent(new logme());
alternatively can generate event registering event in service provider running command
art event:generate
<< automatically add 2 classes you
- you can listen event without creating event handler or registering lister in listeners array.
go event service prover , inside boot function write event , action (not recommended)
event::listen('xxx\events\dosomethingelse', function($event) { dd('handle me :)'); });
- you can queue event or subscribe multiple events within class
Comments
Post a Comment