PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 20, 2022

[FIXED] Laravel 5 Event Handler Not Firing

 February 20, 2022     events, laravel, laravel-5, php     No comments   

Issue

So I'm trying out the new Laravel 5 Event methodology.

In my repository, I'm firing the event "KitchenStored" as so:

//  Events
use App\Events\KitchenStored;

class EloquentKitchen implements KitchenInterface {

    public function store($input) {
        $kitchen        = new $this->kitchen;
        $kitchen->name  = $input['name'];
        $kitchen->save();

        \Event::fire(new KitchenStored($kitchen));

        return $kitchen;
    }

Which successfully fires this event:

<?php namespace App\Events;

use App\Events\Event;

use Illuminate\Queue\SerializesModels;

class KitchenStored extends Event {

    use SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($kitchen)
    {
        $this->kitchen  = $kitchen;
    }

}

However, it doesn't link up to this handler:

<?php namespace App\Handlers\Events;

use App\Events\KitchenStored;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;

class AttachCurrentUserToKitchen {

    /**
     * Create the event handler.
     *
     * @return void
     */
    public function __construct()
    {
        dd('handler');
    }

    /**
     * Handle the event.
     *
     * @param  KitchenStored  $event
     * @return void
     */
    public function handle(KitchenStored $event)
    {
        //
    }

}

which I know because the dd('handler'); isn't fired during the request lifecycle.

I have registered the event with its listener here:

<?php namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider {

    /**
     * The event handler mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        App\Events\KitchenStored::class => [
            App\Handlers\Events\AttachCurrentUserToKitchen::class
        ]
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);
        Event::listen('App\Events\KitchenStored',
                    'App\Handlers\Events\AttachCurrentUserToKitchen');
    }

}

Can anyone explain this process better so I can keep going with the cleanest code I have to date?

Many thanks


Solution

In EventServiceProvider.php, include the leading \ when referencing a class using the ::class notation:

protected $listener = [
    \App\Events\KitchenStored::class => [
      \App\Handlers\Events\AttachCurrentUserToKitchen::class,
    ],
];

You could also add use statements and keep your listener mappings short:

use App\Events\KitchenStored;
use App\Handlers\Events\AttachCurrentUserToKitchen;
...
protected $listener = [
    KitchenStored::class => [
      AttachCurrentUserToKitchen:class,
    ],
];

Or just use the string notation:

protected $listener = [
    'App\Events\KitchenStored' => [
      'App\Handlers\Events\AttachCurrentUserToKitchen',
    ],
];


Answered By - John Berberich
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing