user4877203
user4877203

Reputation: 57

How to get the current controller instance in laravel EventHandler?

I have set some params In my BaseController:

class BaseController{
    public $user;
    public $param;
    ...
    __construct(){
        $this->user = Auth::user();
        $this->param = Input::all();
        ...
    }    
}

Now I can use $this->user to get the user info in every controller.

But now, I want to get the user info in UserHandler@update for log. How to get the instance of current controller? like:

class UserHandler{
    public function onUpdate()
    {
           $instance = SomeClass::getCurrent();
           $user = $instance->user;
    }
}

I don't want to use Auth::user() method. Because that's a complex process in my case.

Upvotes: 1

Views: 1611

Answers (1)

Yevgeniy Afanasyev
Yevgeniy Afanasyev

Reputation: 41430

Normally, everything sits in a container called the Application

$controller = app(\Illuminate\Routing\Route::class)->controller;

And also, there is a nicer way

$controller = request()->route()->controller;

Tested on Laravel 7.29.3

Upvotes: 1

Related Questions