pivar/hashids

Laravel 的 Hashids 桥接器

2.4.0 2016-07-11 19:13 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:38:54 UTC


README

hashids

Laravel 5 为 Hashids API 提供了一个具有多个连接的包装器。 Hashids 是一个小的开源库,可以从数字生成短、唯一、非顺序的 ID。

// Encode integers.
Hashids::encode(4815162342);

// Decode strings.
Hashids::decode('1LLb3b4ck');

// Dependency injection example.
$hashidsManager->encode(911);

Build Status StyleCI Coverage Status Quality Score Latest Version License

安装

使用 Composer 在项目的根目录中要求此包。

$ composer require vinkla/hashids

将服务提供者添加到 config/app.php 中的 providers 数组。

Vinkla\Hashids\HashidsServiceProvider::class

如果您想使用 facade,可以在 config/app.php 中添加对您的别名数组的引用。

'Hashids' => Vinkla\Hashids\Facades\Hashids::class

配置

Laravel Hashids 需要连接配置。要开始,您需要发布所有供应商资产

$ php artisan vendor:publish

这将在您的应用中创建一个 config/hashids.php 文件,您可以修改它以设置配置。同时,请确保检查该包中原始配置文件之间的更改。

默认连接名称

此选项 default 是您可能指定以下哪个连接作为您所有工作的默认连接的地方。当然,您可以使用管理器类同时使用许多连接。此设置的默认值是 main

Hashids 连接

此选项 connections 是为您的应用程序设置每个连接的地方。已包括示例配置,但您可以添加尽可能多的连接。

用法

HashidsManager

这是最感兴趣的类。它绑定到 ioc 容器作为 hashids,并可以使用 Facades\Hashids facade 访问。该类通过扩展 AbstractManager 实现 ManagerInterface。该接口和抽象类都是 Graham Campbell 的 Laravel Manager 包的一部分,因此您可能想要查看该存储库中的文档,了解如何使用管理器类。请注意,返回的连接类始终是 Hashids\Hashids 的实例。

Facades\Hashids

此 facade 会将静态方法调用动态传递到 ioc 容器中的 hashids 对象,默认情况下是 HashidsManager 类。

HashidsServiceProvider

此类不包含任何有趣的公共方法。应将此类添加到 config/app.php 中的提供者数组。此类将设置 ioc 绑定。

示例

在这里,您可以看到这个包是多么简单易用。默认情况下,默认适配器是 main。在配置文件中输入您的认证详情后,它就会正常工作

// You can alias this in config/app.php.
use Vinkla\Hashids\Facades\Hashids;

Hashids::encode(4815162342);
// We're done here - how easy was that, it just works!

Hashids::decode('doyouthinkthatsairyourebreathingnow');
// This example is simple and there are far more methods available.

Hashids 管理器将表现得像 Hashids\Hashids。如果您想调用特定的连接,可以使用连接方法

use Vinkla\Hashids\Facades\Hashids;

// Writing this…
Hashids::connection('main')->encode($id);

// …is identical to writing this
Hashids::encode($id);

// and is also identical to writing this.
Hashids::connection()->encode($id);

// This is because the main connection is configured to be the default.
Hashids::getDefaultConnection(); // This will return main.

// We can change the default connection.
Hashids::setDefaultConnection('alternative'); // The default is now alternative.

如果您像我一样喜欢使用依赖注入而不是 facade,则可以注入管理器

use Vinkla\Hashids\HashidsManager;

class Foo
{
	protected $hashids;

	public function __construct(HashidsManager $hashids)
	{
		$this->hashids = $hashids;
	}

	public function bar($id)
	{
		$this->hashids->encode($id)
	}
}

App::make('Foo')->bar();

文档

此包中还有其他未在此处记录的类。这是因为在包中,此包是 Ivan AkimovHashids 包 的 Laravel 包装器。

许可证

Laravel Hashids 根据 MIT 许可证 (MIT) 许可。