Laravel event RevokeOldToken error
You need import that two class in listener.
use Laravel\Passport\Events\AccessTokenCreated;use Laravel\Passport\Events\RefreshTokenCreated;
I think you didn't import them, so laravel think your listener is expecting that two wrong classes, App\Events\Laravel\Passport\Events\AccessTokenCreated
and App\Events\Laravel\Passport\Events\AccessTokenCreated
tl;dr
Artisan generates an incorrect use
path for the event classes in the listener files so you have to fix them, by removing the App\Events
part from the front.
Explanation
The Generating Events & Listeners subsection under the documentation's Registering Events & Listeners section mentions that the event and listener classes are generated based on the app/Providers/EventServiceProvider.php
class' listen
attribute.
This is a nice feature, but there is a little problem with it: if the array's keys (the event file paths) in the $listen
attribute reference an event from the vendor
folder (Laravel\Passport\Events
in this case) then in the generated listener file, the imported event's path will be prefixed with App\Events\
. This is the thing that you have to remove.
Because of this, the use
path now references a non existing class that doesn't raise an error like "using non existing class", so when the code gets executed and the event is fired, then thanks to the mappings in the EventServiceProvider.php
file, the proper event listener will be found for the event, but when the listener's handle
method is called with the event, it will raise a type error (which is a PHP thing), because the argument's type hinting references a different (non existing) class.
Catching the bug in action
If we dig deep into the framework, we can find the Illuminate/Foundation/Console/EventGenerateCommand.php
class the gets executed when you call php artisan event:generate
. You can see that it parses the EventServiceProvider
class' listen
attribute then starts to generate event and listener files.
The listener generation will happen in the Illuminate/Foundation/Console/ListenerMakeCommand.php
file, that will create the generated file based on the stub found in Illuminate/Foundation/Console/stubs/listener.stub
.
The dummy import path will be replaced in the buildClass
function. The new path has been calculated just before the replace took place and that is where the error happens:
if (! Str::startsWith($event, [ $this->laravel->getNamespace(), 'Illuminate', '\\',])) { $event = $this->laravel->getNamespace().'Events\\'.$event;}
In our case $event
will start with Laravel\Passport\Events
and not with App\
, Illuminate
or \
so the path will be prefixed with App\Events\
. That is the thing you should remove in the generated files.
I hope this is official enough.