bharley / laraveldoctrine-hashids by Panoptisis

Hashid type for Doctrine
86
10
2
Package Data
Maintainer Username: Panoptisis
Package Create Date: 2016-02-27
Package Last Update: 2016-02-27
Language: PHP
License: MIT
Last Refreshed: 2024-11-19 03:12:10
Package Statistics
Total Downloads: 86
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 10
Total Watchers: 2
Total Forks: 1
Total Open Issues: 1

Laravel Doctrine Hashids

This package uses vinkla's Hashids bridge for Laravel to provide a Doctrine type that obfuscates integer IDs using Hashids.

Installation

Before installing this package, install and configure the vinkla/hashids package.

Require this package using composer:

composer require "blake/laraveldoctrine-hashids:~1.0"

The next step is enabling the Doctrine type. If you are using the laravel-doctrine/orm package, add this to your config/doctrine.php file:

    'custom_types'              => [
        // ...
        'hashid' => Blake\Dbal\Types\HashidType::class,
    ],

Usage

Anywhere you are using an integer in your entities you may safely replace with the hashid type:

<?php

namespace App\Entities;

use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 * @ORM\Table
 */
class User
{
    /**
     * @var string
     * @ORM\Id
     * @ORM\Column(type="hashid", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;
    
    // ...
}

Interacting with the entity manager is as you'd expect. Simply replace where you'd use lame integer IDs with their obfuscated version:

$user = EntityManager::find(User::class, 'dlGRfgH73');

The ID will be decoded before it hits the database, and it will be encoded on the way out. This allows you to use performant integer IDs while obfuscating details about your database and making URLs that point to objects slightly prettier.

Configuration

vinkla/hashids allows you to set up multiple "connections". This package will look for a doctrine connection and use main as a fallback if the doctrine connection doesn't exist.