在 Lumen 的何处注册 Facades & Service Providers

Where to register Facades & Service Providers in Lumen

我正在寻找在 Lumen 中添加下面外观的位置。

'JWTAuth' => 'Tymon\JWTAuth\Facades\JWTAuth'

已编辑

还有在bootstrap\app.php

中注册服务提供商的地方
$app->register('Tymon\JWTAuth\Providers\JWTAuthServiceProvider');

请协助。

在您的 bootstrap/app.php 中,确保您已取消评论:

$app->withFacades();

然后,注册你的 class 别名并检查它是否已经存在(否则你的测试会失败):

if (!class_exists('JWTAuth')) {
    class_alias('Tymon\JWTAuth\Facades\JWTAuth', 'JWTAuth');
}

要注册您的 ServiceProvider,请检查您的 bootstrap/app.php:

/*
|--------------------------------------------------------------------------
| 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');

// Add your service provider here
$app->register('Tymon\JWTAuth\Providers\JWTAuthServiceProvider');

更新 #1

我制作了一个 simpel 样板 here 以将 Lumen 与 JWT 和 Dingo 集成。

要使用别名注册外观,转到bootstrap/app.php并取消注释:

$app->withFacades();

... 它指示框架从外观开始。要添加你的门面,只需将它们放在一个数组中并将该数组作为第二个参数传递,同时将第一个参数设置为 true,如下所示:

$app->withFacades(true, [
    'Tymon\JWTAuth\Facades\JWTAuth' => 'JWTAuth',
    'facade' => 'alias',
]);

要注册服务提供商,在同一文件中,向下滚动到相关评论部分并添加以下行:

$app->register(Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class);

在你的bootstrap\app.php

提供商示例

// XML parser service provider
$app->register(\Nathanmac\Utilities\Parser\ParserServiceProvider::class);
// GeoIP
$app->register(\Torann\GeoIP\GeoIPServiceProvider::class);
$app->withEloquent();

别名示例

// SERVICE ALIASES
class_alias(\Nathanmac\Utilities\Parser\Facades\Parser::class, 'Parser');
class_alias(\Torann\GeoIP\Facades\GeoIP::class, 'GeoIP');
$app->withFacades();
...
...
...

祝你好运