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
クラスであるかのように動作します。特定の接続を呼び出したい場合は、次の接続メソッドを使用してそれを行うことができます。
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
にあるドキュメントを参照してください。