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
上的文檔。