toanld / hashids
Laravel的Hashids桥梁
v2.0.2
2023-10-18 09:08 UTC
Requires
- php: ^8.1
- graham-campbell/manager: ^5.0
- hashids/hashids: ^5.0
- illuminate/contracts: ^10.0
- illuminate/support: ^10.0
Requires (Dev)
- graham-campbell/analyzer: ^4.0
- graham-campbell/testbench: ^6.0
- mockery/mockery: ^1.5
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-09-18 11:08:24 UTC
README
Laravel Hashids
为Laravel提供的Hashids桥梁。
// Encode integers. Hashids::encode(4815162342); // Decode strings. Hashids::decode('1LLb3b4ck'); // Dependency injection example. $hashidsManager->encode(911);
安装
使用Composer在项目的根目录中要求此包。
composer require toanld/hashids
配置
Laravel Hashids需要连接配置。要开始,您需要发布所有供应商资产
$ php artisan vendor:publish
这将在您的app中创建一个config/hashids.php
文件,您可以修改它以设置配置。同时,请确保检查此包中原始配置文件之间的更改。
默认连接名称
此选项default
是您可能指定要使用以下哪个连接作为所有工作的默认连接的地方。当然,您可以使用管理器类一次使用多个连接。此设置的默认值是main
。
Hashids连接
此选项connections
是设置应用程序中每个连接的地方。已包含示例配置,但您可以添加您想要的任何数量的连接。
使用
在这里,您可以看到您可能使用此包的示例。开箱即用,默认适配器是main
。在您在配置文件中输入认证详细信息后,它将正常工作
// You can alias this in config/app.php. use Vinkla\Hashids\Facades\Hashids; // We're done here - how easy was that, it just works! Hashids::encode(4815162342); // This example is simple and there are far more methods available. Hashids::decode('doyouthinkthatsairyourebreathingnow');
管理器将表现得像是一个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.
如果您更喜欢使用依赖注入而不是外观,则可以注入管理器
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();
有关如何使用Hashids\Hashids
类的更多信息,请查看hashids/hashids
的文档。