laravel hashids
7.0.0
Laravel 的 Hashids 桥。
// Encode integers.
Hashids:: encode ( 4815162342 );
// Decode strings.
Hashids:: decode ( ' 1LLb3b4ck ' );
// Dependency injection example.
$ hashidsManager -> encode ( 911 );
需要将此包与 Composer 一起放在项目的根目录中。
composer require vinkla/hashids
Laravel Hashids 需要连接配置。首先,您需要发布所有供应商资产:
php artisan vendor:publish
这将在您的应用程序中创建一个config/hashids.php
文件,您可以修改该文件来设置您的配置。另外,请确保在各个版本之间检查此包中原始配置文件的更改。
在此选项default
中,您可以指定您希望将下面的哪个连接用作所有工作的默认连接。当然,您可以使用管理器类同时使用多个连接。此设置的默认值为main
。
此选项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 ' );
管理器的行为就像是HashidsHashids
类。如果你想调用特定的连接,你可以使用connection方法来实现:
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 ();
有关如何使用HashidsHashids
类的更多信息,请查看hashids/hashids
上的文档。