erwane / cakephp-token

在 CakePHP 4 中轻松使用令牌

安装次数: 1,353

依赖关系: 0

建议者: 0

安全: 0

星标: 4

关注者: 3

分支: 2

开放性问题: 0

类型:cakephp-plugin

2.2.0 2023-11-14 15:55 UTC

This package is auto-updated.

Last update: 2024-09-14 17:43:05 UTC


README

描述

这是一个用于生成和读取临时令牌的非常“易于使用”的 cakephp 4 插件。

安装

composer require erwane/cakephp-token:^2.0
bin/cake migrations migrate -p Token

使用

生成令牌

/**
 * Create a token with data and return the id
 * @param  array  $content   an array of custom data
 * @param  string $expire    expire exprimed in '+6 days +2 hours' format
 * @param  int $length Token length
 * @return string            The token id
 */

$myNewTokenId = \Token\Token::generate(array $content, $expire, 8);

获取令牌

// return null (expired or not found) or Token entity
$token = \Token\Token::get($tokenId);

删除令牌

令牌的删除可以忽略,它们将在过期时自动销毁,但在某些情况下,您可能需要立即删除一个令牌。

/**
 * Delete token from id or entity
 * 
 * @param \Token\Model\Entity\Token|string $token Token entity or id
 * @return bool True if token was deleted
 */
$result = \Token\Token::delete($token);

自动清理

每次读取令牌时,都会修剪已过期的令牌。