简体   繁体   English

访问已在 LARAVEL 的构造函数中声明的请求参数

[英]Accessing a request parameter already declared in constructor in LARAVEL

I want to access a Request parameter in my Listener.我想在我的监听器中访问一个请求参数。 I found this solution (to declare it in the constructor) but it should be only initialized in handle method?我找到了这个解决方案(在构造函数中声明它)但它应该只在句柄方法中初始化?

I want to access my request in the onNewUser method我想在onNewUser方法中访问我的请求

my code我的代码

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

public function handle($event)
{
    //
}

public function subscribe($events)
{
    $events->listen(
        'eloquent.creating: App\Models\User',
        'App\Listeners\UserSubscriber@onNewUser'
    );
}

public function onNewUser(User$user){
    userService::userModification($user, 'creating',$this->request);
}

Have you set $request as your class's property?您是否将$request设置为您班级的财产? Like so:像这样:

class Something {

    public $request;

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

    public function onNewUser()
    {
        return $this->request; // That way, you can initialize it here
    }

}

Let me know if this helped.让我知道这是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM