0
votes

I've installed Lumen with Homestead and I am trying to implement Events.

In the documentation it says

.. uncomment the $app->withFacades() call in your bootstrap/app.php file.

To subscribe to an event, you may use the Event::listen method

When I copy-paste the example code from the docs I get the next error

Fatal error: Class 'Event' not found in /home/vagrant/project/...

I couldn't find anything in the Lumen docs why this isn't working.

On a tutorial for Laravel 5 I noticed that an Event created using the artisan make:event command has a use statement like use App\Events\Event; That could be the missing Event class I need, but how should one get it in a Lumen project if that artisan command doesn't exist?

4

4 Answers

3
votes

I am not sure which version of Lumen you are using. In Lumen 5.2 to 5.4, here is how you create events. Lumen 5.4 Events.

app/Events/Event.php (This is totally an optional class, the only importance is the use of 'SerializesModels')

<?php
namespace App\Events;
use Illuminate\Queue\SerializesModels;
abstract class Event
{
  use SerializesModels;
}

app/Events/ExampleEvent.php

<?php
namespace App\Events;
class ExampleEvent extends Event
{
    /**
     * Create a new event instance.
     * @return void
     */
    public function __construct()
    {
     //
    }
}

Now bind the Event class to the Event listener in

app/Providers/EventServiceProvider.php

<?php
namespace App\Providers;
use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     * @var array
     */
    protected $listen = [
       'App\Events\SomeEvent' => [
           'App\Listeners\EventListener',
        ],
    ];
}

Last but not the least, don't forget to uncomment/add the below lines in

bootstrap/app.php

$app->withFacades();

$app->register(App\Providers\EventServiceProvider::class);
0
votes

I believe this is down to :

$app->withFacades();

in bootstrap/app.php not including the event facade as a class alias.

Adding:

class_alias('Illuminate\Support\Facades\Event', 'Event');

to Laravel\Lumen\Application.php::withFacades() fixes the issue but as this is a core Lumen file its not a recommended fix.

0
votes

Might sound silly but have you specified use Event; at the top?

Uncommenting $app->withFacades(); creates the alias already but you still need to import it.

0
votes

The problem it seems the missing App\Events\Event; class that is present in Laravel but not in Lumen scaffolding.

The missing class is simply an abstract empty class:

<?php

namespace App\Events;

abstract class Event
{
    //
}

The Lumen docs have an ambiguous example, hope they will correct this in the future.

<?php

namespace App\Events;

use App\Podcast;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;

class PodcastWasPurchased extends Event
{
    use SerializesModels;

    public $podcast;

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