Laravel 你是经理吗?

Laravel 你是经理吗?,laravel,lumen,Laravel,Lumen,我正在使用库,它使用类型暗示注入AuthManager: use Illuminate\Auth\AuthManager; class Basic extends Authorization { public function __construct(AuthManager $auth, $identifier = 'email') { $this->auth = $auth; $this->identifier = $identifier;

我正在使用库,它使用类型暗示注入AuthManager:

use Illuminate\Auth\AuthManager;
class Basic extends Authorization
{

public function __construct(AuthManager $auth, $identifier = 'email')
    {
        $this->auth = $auth;
        $this->identifier = $identifier;
    }
问题是,如果我使用中间件jwt.auth:

app('Dingo\Api\Routing\Router')->version('v1', ['middleware' => ['jwt.auth'] , 'prefix' => 'api', 'providers' => ['jwt']], function ($api) {
    $api->get('protected', function () {

       $token = JWTAuth::getToken();
       return $token;
    });
});
我得到这个错误:

{"message":"Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\\Auth\\AuthManager","status_code":500,"debug":{"line":839,"file":"\/share\/vendor\/illuminate\/container\/Container.php","class":"Illuminate\\Contracts\\Container\\BindingResolutionException"
{“message”:“在类illumb\\Auth\\AuthManager中无法解析的依赖项解析[参数#0[$app]],“状态代码”:500,“调试”:{“行”:839,“文件”:“\/share\/vendor\/illumb\/container\/container.php”,“类”:“illumb\\Contracts\\container\\BindingResolutionException”

因此,问题是,如何正确地注入AuthManager?为什么$app没有得到解决?

尝试在
引导/app.php
文件中注入
AuthManager

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);

// Injecting goes here
$app->singleton(Illuminate\Auth\AuthManager::class, function ($app) {
    return $app->make('auth');
});
解释 我们知道,如果我们运行
illible\Auth\AuthManager
,将自动解析
illible\Auth\AuthServiceProvider
。请参阅:

Illuminate\Auth\AuthServiceProvider@registerAuthenticator
因此,我们必须先运行此服务提供商,然后才能使用
AuthManager
。但Lumen略有不同。我发现
illighte\Auth\AuthManager
尚未在中注册:

Laravel\Lumen\Application::$availableBindings
当容器想要解析资源时,让Lumen运行得更快是一种黑客行为,请参见:

Laravel\Lumen\Application@make
因此,基本上,如果您想要解析类及其依赖项,可以在使用它之前先注册它的类绑定

使现代化 我们知道

Laravel\Lumen\Application::$availableBindings
属性处于
公共
可见性中,因此这也适用于:

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);

$app->availableBindings['Illuminate\Auth\AuthManager'] = 'registerAuthBindings';
$app->alias('auth', 'Illuminate\Auth\AuthManager');
更新2
我知道如果我们想在Lumen中用实现JWT身份验证,会有很多问题。所以,我做了一个引导(干净的开始)与此库集成良好的Lumen应用程序。请查看。我将添加关于哪一个应用程序以及为什么以后要更改代码的说明。干杯。

尝试在您的
引导/app.php
文件中插入
AuthManager

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);

// Injecting goes here
$app->singleton(Illuminate\Auth\AuthManager::class, function ($app) {
    return $app->make('auth');
});
解释 我们知道,如果我们运行
illible\Auth\AuthManager
,将自动解析
illible\Auth\AuthServiceProvider
。请参阅:

Illuminate\Auth\AuthServiceProvider@registerAuthenticator
因此,我们必须先运行此服务提供商,然后才能使用
AuthManager
。但Lumen略有不同。我发现
illighte\Auth\AuthManager
尚未在中注册:

Laravel\Lumen\Application::$availableBindings
当容器想要解析资源时,让Lumen运行得更快是一种黑客行为,请参见:

Laravel\Lumen\Application@make
因此,基本上,如果您想要解析类及其依赖项,可以在使用它之前先注册它的类绑定

使现代化 我们知道

Laravel\Lumen\Application::$availableBindings
属性处于
公共
可见性中,因此这也适用于:

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);

$app->availableBindings['Illuminate\Auth\AuthManager'] = 'registerAuthBindings';
$app->alias('auth', 'Illuminate\Auth\AuthManager');
更新2
我知道如果我们想在Lumen中用实现JWT身份验证,会有很多问题。所以,我做了一个引导(干净的开始)Lumen应用程序与此库很好地集成。请查看。我将添加关于哪一个以及为什么我们以后应该更改代码的说明。干杯。

我在注册现有Laravel
SessionService Provider时,使用相同的无法解决的
$app
变量使用SessionManager
体验到了这一点

在阅读了@krisanalfa的答案后,我试图偷看一下
$availableBindings
的值,这些值可以在
Application.php
中找到,它看起来是这样的:

public $availableBindings = [
      'auth' => 'registerAuthBindings',
      'auth.driver' => 'registerAuthBindings',
      'Illuminate\Auth\AuthManager' => 'registerAuthBindings',

      'Illuminate\Contracts\Cache\Factory' => 'registerCacheBindings',
      'Illuminate\Contracts\Cache\Repository' => 'registerCacheBindings',
      ....
];
每个键的值表示用于加载实现的方法,这些实现也位于
应用程序.php

如果需要加载配置并注册绑定:

protected function registerAuthBindings()
{
    $this->singleton('auth', function () {
        return $this->loadComponent('auth', 'Illuminate\Auth\AuthServiceProvider', 'auth');
    });

    $this->singleton('auth.driver', function () {
        return $this->loadComponent('auth', 'Illuminate\Auth\AuthServiceProvider', 'auth.driver');
    });

   ...
}
但是,如果服务不需要任何配置,您只需按如下方式注册:

protected function registerEventBindings()
{
    $this->singleton('events', function () {
        $this->register('Illuminate\Events\EventServiceProvider');

        return $this->make('events');
    });
}
撰写本文时的资料来源:


希望这对以后的其他人有所帮助。这花了我很多时间。

我在注册现有的Laravel
SessionService Provider时,使用相同的无法解决的
$app
变量使用了
SessionManager
体验到了这一点

在阅读了@krisanalfa的答案后,我试图偷看一下
$availableBindings
的值,这些值可以在
Application.php
中找到,它看起来是这样的:

public $availableBindings = [
      'auth' => 'registerAuthBindings',
      'auth.driver' => 'registerAuthBindings',
      'Illuminate\Auth\AuthManager' => 'registerAuthBindings',

      'Illuminate\Contracts\Cache\Factory' => 'registerCacheBindings',
      'Illuminate\Contracts\Cache\Repository' => 'registerCacheBindings',
      ....
];
每个键的值表示用于加载实现的方法,这些实现也位于
应用程序.php

如果需要加载配置并注册绑定:

protected function registerAuthBindings()
{
    $this->singleton('auth', function () {
        return $this->loadComponent('auth', 'Illuminate\Auth\AuthServiceProvider', 'auth');
    });

    $this->singleton('auth.driver', function () {
        return $this->loadComponent('auth', 'Illuminate\Auth\AuthServiceProvider', 'auth.driver');
    });

   ...
}
但是,如果服务不需要任何配置,您只需按如下方式注册:

protected function registerEventBindings()
{
    $this->singleton('events', function () {
        $this->register('Illuminate\Events\EventServiceProvider');

        return $this->make('events');
    });
}
撰写本文时的资料来源:


希望这对以后的其他人有所帮助。这花了我很多时间。

$app->singleton(illumb\Auth\AuthManager::class,意味着无论何时使用AuthManager,它都将使用相同的实例,但是您能解释一下代码$app->make('Auth')吗?嗨@simo,我更新了答案。是否足够清楚?如果你需要更清楚的解释,请告诉我,如果你愿意,我将帮助你深入了解Lumen内核。非常感谢Krisa的解释,我到达办公室后会尝试解决方案,我对OOP非常有经验,但需要了解更多关于Laravel和Lumen框架的信息。可以吗ot在架构方面为他们找到好的资源。@simo很高兴帮助你,SirHi@simo,我更新了答案。很抱歉回复太晚,我刚到家。如果你需要什么,可以通过我的电子邮件与我联系。我真的很高兴帮助人们。$app->singleton(illumb\Auth\AuthManager::class,意味着无论何时使用AuthManager,它都将使用相同的实例,但您能否解释代码$app->make('Auth'))?嗨@simo,我更新了答案。是否足够清楚?如果你需要更清楚的解释,请告诉我,如果你愿意,我将帮助你深入了解Lumen内核。非常感谢Krisa的解释,我到达办公室后会尝试解决方案,我对OOP非常有经验,但需要了解更多关于Laravel和Lumen框架的信息。可以吗我们无法为他们找到关于体系结构的好资源。@simo很高兴能帮助你,SirHi@simo,我更新了答案。很抱歉回复太晚,我刚到家。如果你需要的话