zeroo0ooweb / hashids
Laravel 的 Hashids 桥接器
dev-main
2021-09-17 16:48 UTC
Requires
- php: ^7.3 || ^8.0
- graham-campbell/manager: ^4.4
- hashids/hashids: ^4.1
- illuminate/contracts: ^8.0
- illuminate/support: ^8.0
Requires (Dev)
- graham-campbell/analyzer: ^3.0
- graham-campbell/testbench: ^5.4
- mockery/mockery: ^1.3
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-15 05:08:00 UTC
README
Laravel Hashids
为 Laravel 提供的 Hashids 桥接器。
// Encode integers. Hashids::encode(4815162342, 'your-salt-string'); // Decode strings. Hashids::decode('1LLb3b4ck', 'your-salt-string'); // Dependency injection example. $hashidsManager->encode(911);
安装
使用 Composer 在项目的根目录中要求此包。
composer require zeroo0ooweb/hashids
配置
Laravel Hashids 需要连接配置。要开始,你需要发布所有供应商资产
$ php artisan vendor:publish
这将在你的应用中创建一个 config/hashids.php
文件,你可以修改它来设置你的配置。同时,请确保检查该包中原始配置文件在版本间的更改。
默认连接名称
此选项 default
是你指定要使用的以下哪个连接作为所有工作的默认连接的地方。当然,你可以同时使用多个连接,使用管理器类。此设置的默认值是 main
。
Hashids 连接
此选项 connections
是为你的应用程序设置每个连接的地方。已包含示例配置,但你可能添加你想要的任意多个连接。
用法
在这里,你可以看到如何使用此包的示例。默认情况下,默认适配器是 main
。在配置文件中输入你的认证详细信息后,它将正常工作。
// You can alias this in config/app.php. use zeroo0ooweb\Hashids\Facades\Hashids; // We're done here - how easy was that, it just works! Hashids::encode(4815162342, 'your-salt-string'); // This example is simple and there are far more methods available. Hashids::decode('doyouthinkthatsairyourebreathingnow');
管理器将表现得像 Hashids\Hashids
类。如果你想调用特定的连接,你可以使用连接方法
use zeroo0ooweb\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.
如果你更愿意使用依赖注入而不是外观,则可以注入管理器
use zeroo0ooweb\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();
有关如何使用 Hashids\Hashids
类的更多信息,请查看 hashids/hashids
上的文档。