Laravel 5.2 : How to access Request & Session Classes from own Event Listener?
Asked Answered
R

1

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.

Reticulation answered 8/4, 2016 at 7:18 Comment(0)
G
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!');
}
Garboard answered 8/4, 2016 at 7:20 Comment(9)
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?Provincetown
I don't know how You used to handle requests in routes.php file. Please show some example and then I can say something :)Eachern
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! :))Provincetown
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: 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 :)Eachern
So which is the preferred (or) proper way? Your explained way or that dependency injections way? :DProvincetown
It depends. :) But in my own opinion dependency injection is cleaner solution.Eachern
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 ThanksssProvincetown
is it possible to pass Illuminate\Http\Request to an event? I am NOT talking about the listener here.Sissel
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.Taegu

© 2022 - 2024 — McMap. All rights reserved.