我正在寻找在Lumen中添加下面外观的位置。
'JWTAuth' => 'Tymon\JWTAuth\Facades\JWTAuth'
编辑过的
还有在bootstrap\app.php
中注册服务提供程序的位置
$app->register('Tymon\JWTAuth\Providers\JWTAuthServiceProvider');
请协助。请确保在您的bootstrap/app.php
文件中已取消注释:
$app->withFacades();
然后,注册您的类别名并检查它是否已存在(否则您的测试将会出错):
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');
我制作了一个简单的样板在这里,用于将Lumen与JWT和Dingo集成。
要为门面注册别名,请转到bootstrap/app.php
并取消注释:
$app->withFacades();
...它指示框架从Facade开始。要添加您的Facades,只需将它们放在数组中,并将该数组作为第二个参数传递,同时将第一个参数设置为true,如下所示:
$app->withFacades(true, [
'Tymon\JWTAuth\Facades\JWTAuth' => 'JWTAuth',
'facade' => 'alias',
]);
注册服务提供者,在同一个文件中,向下滚动到相关的评论部分,然后添加以下行:
$app->register(Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class);
// XML parser service provider
$app->register(\Nathanmac\Utilities\Parser\ParserServiceProvider::class);
// GeoIP
$app->register(\Torann\GeoIP\GeoIPServiceProvider::class);
$app->withEloquent();
Alias示例
// SERVICE ALIASES
class_alias(\Nathanmac\Utilities\Parser\Facades\Parser::class, 'Parser');
class_alias(\Torann\GeoIP\Facades\GeoIP::class, 'GeoIP');
$app->withFacades();
...
...
...
祝你好运
class_alias
使Facade可以在全局命名空间中调用。而且...问题也问到了“在哪里注册Facade”,而不是“如何创建Facade”。我认为你需要检查要别名的类,它应该是Facade
本身的扩展。如果你想创建自己的Facade
,你可以阅读这个。 - krisanalfaclass_alias('EasySlug', 'EasySlug\EasySlug\EasySlugFacade');
和$app->register('EasySlug\EasySlug\EasySlugServiceProvider');
。现在我得到了错误:Class 'EasySlug' not found
。如何解决? - w3spiclass_alias('EasySlug\EasySlug\EasySlugFacade', 'EasySlug');
而不是class_alias('EasySlug', 'EasySlug\EasySlug\EasySlugFacade');
。更多信息请参考这里。class_alias
函数的第一个参数是原始类名,而不是别名。希望能对你有所帮助。 - krisanalfaclass_alias
破坏了我的PHPUnit测试,我有些跑题了。使用class_exists
防止多次执行class_alias
。 - Peter Mellett