4

I am using vinkla/hashids and i have followed the following steps

  1. composer require vinkla/hashids
  2. Add the service provider to config/app.php in the providers array
  3. If you want you can use the facade. Add the reference in config/app.php to your aliases array.
  4. php artisan vendor:publish this step does not create hashid.php in config file
  5. use Vinkla\Hashids\Facades\Hashids;
  6. Hashids::encode(4815162342);

And i get error that hashids class not found

Awais Mushtaq
  • 633
  • 1
  • 10
  • 23
  • Did you alias `Vinkla\Hashids\Facades\Hashids` in config/app.php or not? – Jared Eitnier Aug 11 '16 at 13:53
  • yes i mentioned it in step 3 – Awais Mushtaq Aug 11 '16 at 13:56
  • 1
    Have you tried `composer dump-autoload` at all? Might help if you show the controller or library class that is trying to generate a hashid. – Jared Eitnier Aug 11 '16 at 13:59
  • yes i have tried everything . composer update, composer install and dump auto-load – Awais Mushtaq Aug 11 '16 at 14:04
  • Just for Test purposes, try manually importing the classes by adding explicit `require_once __DIR__ . $pathToVinklaHashidsClass;`. From your observation after that; you may debug/troubleshoot from there. – Poiz Aug 11 '16 at 14:33
  • do u thing that its problem in step 4 as that command does not generate hashid.php in config ? – Awais Mushtaq Aug 11 '16 at 14:42
  • No... actually just wanted to say that the files is actually created... and you can modify it... check your `$laravelSite/config` Folder. You will notice that a File called `hashids.php` exists there. Now, you can simple configure everything in this file.... – Poiz Aug 11 '16 at 14:52

3 Answers3

3

It seems that the provider is not booting.

Try to do this:

php artisan config:clear
php artisan clear-compiled

The first will clear any cached config files, and the later will clear the services cache.

It worked for me, hope it works for you too.

I found the solution here: Laravel 5.2 Service provider not booting

Community
  • 1
  • 1
Diguin
  • 835
  • 8
  • 17
0

Try checking inside your $laravelSite/config Directory to see if you find a file called hashids.php...

In your Controller; try also to import the the Hashids Class manually like so:

<?php
    namespace App\Http\Controllers;

    use Vinkla\Hashids\Facades\Hashids;

    class SampleClass extends  {

        public function testHashID(){
            $h1 = Hashids::encode(4815162342);
            var_dump($h1);
            $h2 = Hashids::decode('oaobgb-rnar');
            var_dump($h2);             
        }
    }

And by the way; if you don't see hashids.php within your $laravelSite/config Directory; you may try to manually create it. The file just returns an array of Configuration Settings... the content to the file looks like so:

    /*
     * This file is part of Laravel Hashids.
     *
     * (c) Vincent Klaiber <hello@vinkla.com>
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */

    return [

        /*
        |--------------------------------------------------------------------------
        | Default Connection Name
        |--------------------------------------------------------------------------
        |
        | Here you may specify which of the connections below you wish to use as
        | your default connection for all work. Of course, you may use many
        | connections at once using the manager class.
        |
        */

        'default' => 'main',

        /*
        |--------------------------------------------------------------------------
        | Hashids Connections
        |--------------------------------------------------------------------------
        |
        | Here are each of the connections setup for your application. Example
        | configuration has been included, but you may add as many connections as
        | you would like.
        |
        */

        'connections' => [

            'main' => [
                'salt' => 'your-salt-string',
                'length' => 'your-length-integer',
                'alphabet' => 'your-alphabet-string',
            ],

            'alternative' => [
                'salt' => 'your-salt-string',
                'length' => 'your-length-integer',
                'alphabet' => 'your-alphabet-string',
            ],

        ],

    ];
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • @AwaisMushtaq So what errors are you getting now? – Poiz Aug 12 '16 at 09:36
  • Class 'Hashids' not found – Awais Mushtaq Aug 12 '16 at 09:46
  • @AwaisMushtaq Go it step by step. *Step 1:)* **Remove all references Hashids in the `$config/app.php` File**. *Step 2:)* **Remove the entry "require vinkla/hashids" from your composer.json File and also manually find and delete the hashids Folder within your vendors Folder.** *Step 3:* **Run:** `composer require vinkla/hashids` **from command line**. Once that is done: *Step 5:)* **Add** `Vinkla\Hashids\HashidsServiceProvider::class` **to your** `config/app.php` **and then run** `php artisan vendor:publish` **and** `composer dump-autoload` – Poiz Aug 12 '16 at 09:59
0

To encode just do this

\Hashids::encode($characters)

And to decode

\Hashids::decode($characters)
aphoe
  • 2,586
  • 1
  • 27
  • 31