vinkla/laravel-hashids

repository·master·Indexed 24 days ago

https://github.com/vinkla/laravel-hashids

A Laravel bridge for the Hashids library that allows developers to encode and decode integers into short, unique, non-sequential strings. It provides a Hashids facade, HashidsManager for dependency injection, and support for multiple connections via configuration in config/hashids.php.

Tokens
1.1K
Snippets
4
Records
10
Agent score
84%

What's inside vinkla-laravel-hashids

  1. Configure Laravel Hashids

    master

    To configure your Hashids settings, publish the vendor assets to your application. This creates a config/hashids.php file where you can define your connections and the default connection.

    Key configuration options in config/hashids.php:

    • default: Specifies which connection to use by default (defaults to main).
    • connections: An array where you define multiple Hashids connections for your application.
    php artisan vendor:publish
  2. Publish the Hashids configuration file

    master

    To customize the Hashids settings, you can publish the package's configuration file to your application's config directory. This allows you to manage your salt, length, and alphabet settings within your own codebase.

    Run the following Artisan command:

    php artisan vendor:publish --provider="Vinkla\Hashids\HashidsServiceProvider"
  3. Use HashidsManager via Dependency Injection

    master

    Instead of using Facades, you can inject Vinkla\Hashids\HashidsManager into your class constructors. The manager behaves like a standard Hashids\Hashids class.

    use Vinkla\Hashids\HashidsManager;
    
    class Foo
    {
        protected $hashids;
    
        public function __construct(HashidsManager $hashids)
        {
            $this->hashids = $hashids;
        }
    
        public function bar($id)
        {
            $this->hashids->encode($id);
        }
    }
    
    // Resolve and use
    App::make('Foo')->bar();
  4. Encode and Decode using the Hashids Facade

    master

    The easiest way to use the package is via the Vinkla\Hashids\Facades\Hashids facade. Once configured, you can encode integers into hashids and decode hashids back into integers.

    use Vinkla\Hashids\Facades\Hashids;
    
    // Encode integers
    Hashids::encode(4815162342);
    
    // Decode strings
    Hashids::decode('doyouthinkthatsairyourebreathingnow');
  5. Manage multiple Hashids connections

    master

    The package supports multiple connections. You can switch between connections using the connection() method on the facade. If a connection is set as the default in your config, calling connection() without arguments or calling methods directly on the facade will use that default connection.

    Methods for connection management:

    • connection($name): Selects a specific connection by name.
    • getDefaultConnection(): Returns the name of the current default connection.
    • setDefaultConnection($name): Changes the default connection for the current request.
  6. Use HashidsManager to access Hashids instances

    master

    The HashidsManager is the central entry point for managing multiple Hashids configurations. It extends AbstractManager, allowing you to resolve specific Hashids instances by their configuration name. Once an instance is resolved, you can use the following methods provided by the underlying Hashids library:

    • encode(mixed ...$numbers): Encodes one or more numbers into a hash string.
    • decode(string $hash): Decodes a hash string back into an array of numbers.
    • encodeHex(string $str): Encodes a string into a hex hash.
    • decodeHex(string $hash): Decodes a hex hash back into a string.
  7. Service container bindings for Hashids

    master

    The package registers several bindings in the Laravel service container. You can resolve these using the following keys:

    • hashids: Resolves to the HashidsManager. This is the primary entry point for managing multiple hashid connections.
    • hashids.factory: Resolves to the HashidsFactory. Used for creating new Hashids instances.
    • hashids.connection: Resolves to a Hashids instance (the default connection). This is the most common way to access the hashing and decoding functionality directly.
  8. Access the Hashids connection via the Hashids facade

    master
    If you need to interact directly with the underlying Hashids\Hashids instance (for example, to access specific instance methods not exposed by the facade), you can use the connection() method. This method allows you to retrieve the service instance, optionally by a specific connection name.