35

I Am looking for where to add the facade below in Lumen.

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

EDITED

Also where to register service provider in bootstrap\app.php

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

Please assist.

Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96

3 Answers3

65

In your bootstrap/app.php, make sure you've un-commented:

$app->withFacades();

Then, register you class alias and check if it already exists (else your tests will break):

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

To register your ServiceProvider, check your 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');

Update #1

I made a simpel boilerplate here to integrate Lumen with JWT and Dingo.

Daan
  • 7,685
  • 5
  • 43
  • 52
krisanalfa
  • 6,268
  • 2
  • 29
  • 38
  • 1
    actually it doesn't create Facade but class_alias, and even if in most cases it desn't make difference there are some laravel packages that expect given Facade to exist and fails, now I'm trying to figure out how to make it work – zakius Sep 08 '15 at 07:00
  • 1
    You may see [this](https://github.com/laravel/lumen-framework/blob/3836124d8bc2f68e9ac94cc5e5e2b173b0202a95/src/Application.php#L852). The method use `class_alias` to make Facade can be called in global namespace. And... The question also asked about 'Where to register facade' not 'how to make facade'. I think you have to check the class you want to alias, it's should be an extend of `Facade` itself. You may read [this](http://laravel.com/docs/5.1/facades) if you want to create your own `Facade`. – krisanalfa Sep 08 '15 at 09:43
  • @KrisanAlfaTimur I'm trying to use [this package](https://github.com/viraj-khatavkar/easy-slug). I added `class_alias('EasySlug', 'EasySlug\EasySlug\EasySlugFacade');` and `$app->register('EasySlug\EasySlug\EasySlugServiceProvider');`. Now I get the error : `Class 'EasySlug' not found`. How to solve it ? – w3spi Nov 15 '15 at 19:37
  • @Zl3n Hi, I think you're just misunderstood, use `class_alias('EasySlug\EasySlug\EasySlugFacade', 'EasySlug');` instead of `class_alias('EasySlug', 'EasySlug\EasySlug\EasySlugFacade');`. For more information read [this](http://php.net/manual/en/function.class-alias.php). The first argument of `class_alias` function is the original classname, not the alias. Hope this helps. – krisanalfa Nov 16 '15 at 02:52
  • 6
    Bit of a tangent, I ended up here because `class_alias` broke my PHPUnit tests. Guard against the `class_alias` being executed multiple times with `class_exists`. – Peter Mellett Nov 25 '15 at 15:40
  • Thanks @Peter, your comment has been very useful to me. – whoan Sep 26 '16 at 04:21
  • Additionally, check out [this too](https://gist.github.com/mabasic/21d13eab12462e596120) – ProfNandaa Feb 08 '17 at 21:29
23

To register a facade with an alias, go to bootstrap/app.php and uncomment:

$app->withFacades();

... it instructs the framework to start with facades. To add your facades, just put them in an array and pass the array as a second argument, while setting the first argument to true, as follows:

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

To register a service provider, in the same file, scroll down to a relevant comment section and add the following line:

$app->register(Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class);
qwaz
  • 1,285
  • 4
  • 23
  • 47
4

In your bootstrap\app.php

Example for Provider

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

Example for Alias

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

Good luck

llioor
  • 5,804
  • 4
  • 36
  • 44