สะพาน Hashids สำหรับ Laravel
// 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