在Laravel 5.2中使用vinkla / hashids包

时间:2016-08-11 13:41:45

标签: php laravel hash laravel-5.2 hashids

我正在使用vinkla / hashids,我已按照以下步骤进行操作

  1. 作曲家需要vinkla / hashids
  2. 将服务提供商添加到providers数组中的config / app.php
  3. 如果你想要,你可以使用立面。将config / app.php中的引用添加到别名数组中。
  4. php artisan vendor:发布此步骤不会在配置文件中创建hashid.php
  5. 使用Vinkla \ Hashids \ Facades \ Hashids;
  6. Hashids ::编码(4815162342);
  7. 我得到没有找到哈希类的错误

3 个答案:

答案 0 :(得分:3)

似乎提供商没有启动。

尝试这样做:

php artisan config:clear
php artisan clear-compiled

第一个将清除所有缓存的配置文件,后者将清除服务缓存。

它对我有用,希望它也适合你。

我在这里找到了解决方案:Laravel 5.2 Service provider not booting

答案 1 :(得分:0)

尝试在$laravelSite/config目录中查看是否找到名为hashids.php的文件...

在您的控制器中;还尝试手动导入Hashids类,如下所示:

<?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);             
        }
    }

顺便说一下;如果您在hashids.php目录中看不到$laravelSite/config;您可以尝试手动创建它。该文件只返回一个配置设置数组...该文件的内容如下所示:

    /*
     * 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',
            ],

        ],

    ];

答案 2 :(得分:0)

要编码,只需执行此操作

\Hashids::encode($characters)

并解码

\Hashids::decode($characters)