6

In Laravel 5.2, i have added my Event Listener (into app\Providers\EventServiceProvider.php), like:

protected $listen = [
  'Illuminate\Auth\Events\Login' => ['App\Listeners\UserLoggedIn'],
];

Then generated it:

php artisan event:generate

Then in the Event Listener file itself app/Listeners/UserLoggedIn.php, it's like:

<?php

namespace App\Listeners;

use App\Listeners\Request;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Events\Login;

class UserLoggedIn
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param  Login  $event
     * @return void
     */
    public function handle(Login $event, Request $request)
    {
        $request->session()->put('test', 'hello world!');
    }
}

This shows me following Errors:

ErrorException in UserLoggedIn.php line 28:
Argument 2 passed to App\Listeners\UserLoggedIn::handle() must be an instance of App\Listeners\Request, none given

What did i miss, or how can i solve this please?

  • Ultimately, i need to write into Laravel Sessions once the User has logged in.

Thank you all.

夏期劇場
  • 17,821
  • 44
  • 135
  • 217

1 Answers1

17

You are trying to initialize App\Listeners\Request; but it should be Illuminate\Http\Request. Also this might not work, so for plan B use this code:

public function handle(Login $event)
{
    app('request')->session()->put('test', 'hello world!');
}

Dependency Injection Update:

If You want to use dependency injection in events, You should inject classes through constructor like so:

public function __construct(Request $request)
{
    $this->request = $request;
}

Then in handle method You can use local request variable which was stored in constructor:

public function handle(Login $event)
{
    $this->request->session()->put('test', 'hello world!');
}
Giedrius Kiršys
  • 5,154
  • 2
  • 20
  • 28
  • Woww that works! Thanks!! --- So that means, i can NOT use the way i handled the Request and Sessions like it normal `routes.php` file. Instead, once inside the Event Handlers, i have to do your way. Am i right please? – 夏期劇場 Apr 08 '16 at 07:25
  • I don't know how You used to handle requests in `routes.php` file. Please show some example and then I can say something :) – Giedrius Kiršys Apr 08 '16 at 07:27
  • I mean, in `routes.php`, when i want to access to Requests & Sessions, i use like: `Route::get('home', function(Request $request){ $request->session()->put('test', 'hello world!'); });`. Ofcos which is inside the `web` middleware. (Or, does it means, these are possible there because they are inside `web` middleware?) Thanks again! :)) – 夏期劇場 Apr 08 '16 at 07:32
  • 2
    This is the example of dependency injection. It is a really broad topic, but what I can say - You can use it. You can read more about it here: https://laravel.com/docs/master/container#introduction. In event handlers, if You want to use dependency injections, You have to use it in constructor method like so: `public function __construct(Request $request)` and then save that object to class variable. After that, You will be able to use it in `handle` method. Sorry for the confusion, but dependency injection is a pretty hard to explain thing :) – Giedrius Kiršys Apr 08 '16 at 07:38
  • So which is the preferred (or) proper way? Your explained way or that dependency injections way? :D – 夏期劇場 Apr 08 '16 at 07:50
  • 2
    It depends. :) But in my own opinion dependency injection is cleaner solution. – Giedrius Kiršys Apr 08 '16 at 07:51
  • Then please kindly edit your answer to reflect the DE way. So that i can mark it as better answer than this. (Coz i also prefer that way, as im used to it.) :D Thanksss – 夏期劇場 Apr 08 '16 at 07:55
  • is it possible to pass Illuminate\Http\Request to an event? I am NOT talking about the listener here. – Aftab Naveed Jun 30 '17 at 09:08
  • The DI method is much more important with extensions to Laravel such as Octane, that has multiple instances of the entire application running in the same memory space, and you must avoid having them interacting through the global services provided by at `app()` funtion. – Jason Jun 11 '21 at 10:29