If you want to create a JWT instance, call the create
method:
use LittleApps\LittleJWT\Facades\LittleJWT;
use LittleApps\LittleJWT\Build\Builder;
$jwt = LittleJWT::create(function (Builder $builder) {
// ...
});
More information on the JWT instance can be found under The JWT page.
If you want to create a token, simply cast the JWT instance to a string:
use LittleApps\LittleJWT\Facades\LittleJWT;
use LittleApps\LittleJWT\Build\Builder;
$jwt = LittleJWT::create(function (Builder $builder) {
// ...
});
$token = (string) $jwt;
// $token = "ey...";
The first parameter accepted by either method is a callback function. The callback receives a LittleApps\LittleJWT\Build\Builder
instance, which is used to specify what claims to include in the JWT. Further documentation on the Builder
instance is located here.
The following demonstrates passing an anonymous function to create a token:
use LittleApps\LittleJWT\Facades\LittleJWT;
use LittleApps\LittleJWT\Build\Builder;
$jwt = LittleJWT::create(function (Builder $builder) {
$builder
->abc('def', true)
->ghi('klm')
->nop('qrs', false);
});
See Buildables for more information on utilizing buildables as an alternative to anonymous functions.
The second parameter indicates if the default buildable should also be used to create the token. If the second parameter is not specified, it defaults to true
. Claims specified in the callback parameter will be added after and possibly override the default claims.
use LittleApps\LittleJWT\Facades\LittleJWT;
use LittleApps\LittleJWT\Build\Builder;
// Applies the default builder
$jwt = LittleJWT::create(function (Builder $builder) {
/* ... */
}, true);
// Also applies the default builder
$jwt = LittleJWT::create(function (Builder $builder) {
/* ... */
});
// Ignores the default builder
$jwt = LittleJWT::create(function (Builder $builder) {
/* ... */
}, false);
See Buildables for more information on claims added by the default buildable and changing the default buildable.