Php Laravel运行时接口绑定取决于经过身份验证的用户

Php Laravel运行时接口绑定取决于经过身份验证的用户,php,laravel,containers,Php,Laravel,Containers,我有两个实现接口的类,如下所示: interface SomeInterface { public function init(); } class ServiceOne implements SomeInterface { public function init() { // ... } // ... } class ServiceTwo implements SomeInterface { public function ini

我有两个实现接口的类,如下所示:

interface SomeInterface
{
    public function init();
}

class ServiceOne implements SomeInterface
{
    public function init() {
        // ...
    }

    // ...
}

class ServiceTwo implements SomeInterface
{
    public function init() {
        // ...
    }


    // ...
}
$this->app->bind('<NAMESPACE>\SomeInterface', '<NAMESPACE>\ServiceOne');
我可以使用如下服务提供商将一个或另一个绑定到Laravel中的接口:

interface SomeInterface
{
    public function init();
}

class ServiceOne implements SomeInterface
{
    public function init() {
        // ...
    }

    // ...
}

class ServiceTwo implements SomeInterface
{
    public function init() {
        // ...
    }


    // ...
}
$this->app->bind('<NAMESPACE>\SomeInterface', '<NAMESPACE>\ServiceOne');
$this->app->bind('\SomeInterface','\ServiceOne');
或:

$this->app->bind('\SomeInterface','\ServiceTwo');
但是我想在运行时根据数据库中与经过身份验证的用户链接的一个值集执行此操作,因此我需要获得该用户并确定使用哪个

我曾尝试使用服务提供商的引导方法获取用户,然后绑定接口,但我无法在那里获取用户的句柄。始终返回null

我还尝试使用before中间件,并尝试直接从请求中获取用户,但这也会返回null


我认为这是一件非常标准的事情,应该如何做呢?

我认为中间件是处理这一问题的合适场所。但奇怪的是,当时无法抓住用户。这正是auth中间件的工作方式,因此您应该让登录用户在该点可用。我尝试使用before和after中间件,试图从请求中获取用户总是返回null。我必须将2个服务传递给一个控制器,然后决定实际使用哪一个,然后调用init方法,该方法将完成构造函数所做的大部分工作。我相信在拉雷维尔会有更好的方法。