Git Product home page Git Product logo

hashidsbundle's Introduction

SensioLabsInsight Scrutinizer

HashidsBundle

Integrates hashids/hashids in a Symfony2 project.

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

    composer require roukmoute/hashids-bundle

This command requires you to have Composer installed globally.

Enable the Bundle

Then, enable the bundle by adding the following line in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // …
            new Roukmoute\HashidsBundle\RoukmouteHashidsBundle()
        );
        // …

The configuration looks as follows :

roukmoute_hashids:

    # if set, the hashids will differ from everyone else's
    salt:               ""

    # if set, will generate minimum length for the id
    # 0 — meaning hashes will be the shortest possible length
    min_hash_length:    0

    # if set, will use only characters of alphabet string
    alphabet:           "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

    # if set to true, guess automatically hashid
    autowire:           false

Usage

$hashids = $this->get('hashids');

Next it's the same things of official documentation.

Other Features

The Roukmoute\HashidsBundle\Hashids has extra features:

$minHashLength = 42;

// Edit the minimum hash length.
$this->get('hashids')->setMinHashLength($minHashLength)->encode(1, 2, 3);

// Encode with a custom minimum hash length.
$this->get('hashids')->encodeWithCustomHashLength($minHashLength, 1, 2, 3);

Hashids Converter

Converter Name: hashids.converter

The hashids converter attempts to convert request hashid attributes to a id for fetch a Doctrine entity.

For specify to use hashids converter just add "id" = "hashid" in options.

/**
 * @Route("/users/{hashid}")
 * @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"id" = "hashid"})
 */
public function getAction(User $user)
{
}

You could have several hashids one the same URL. Just finish your word option with "hashid":

/**
 * @Route("/users/{userHashid}/status/{statusHashid}")
 * @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"id" = "userHashid"})
 * @ParamConverter("status", class="RoukmouteBundle\Entity\Notification", options={"id" = "statusHashid"})
 */
public function getAction(User $user, Status $status)
{
}

Defining Hashid Automatically (Autowiring)

Autowiring allows to guess hashid with minimal configuration. It automatically resolves the variable in route. The ParamConverter component will be able to automatically guess the hashid when configuration has autowire to true.

roukmoute_hashids:
    autowire: true

The autowiring subsystem will detect the hashid.

Base on the example above:

/**
 * @Route("/users/{hashid}")
 * @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"id" = "hashid"})
 */
public function getAction(User $user)
{
}

Now you can do simply that:

/**
 * @Route("/users/{hashid}")
 */
public function getAction(User $user)
{
}

Or can directly use id now !

/**
 * @Route("/users/{id}")
 */
public function getAction(User $user)
{
}

As you can see, the autowiring feature reduces the amount of configuration required to define a hashid.

Twig Extension

Usage

{{ path('users.show', {'hashid': user.id | hashids_encode }) }}
{{ app.request.query.get('hashid') | hashids_decode }}

hashidsbundle's People

Contributors

hipio avatar roukmoute avatar stephledev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.