【问题标题】:url id masking with hashids : Connection [App\User] not configured带有哈希值的 url id 掩码:未配置连接 [App\User]
【发布时间】:2020-06-30 19:36:22
【问题描述】:

我正在关注 ben sampo 关于 url 屏蔽的教程,但收到错误连接 [App\User] 未配置。

配置中的app.php

        'Hashids' => Vinkla\Hashids\Facades\Hashids::class,

hashids.php

 'connections' => [

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

    'alternative' => [
        'salt' => 'your-salt-string',
        'length' => 'your-length-integer',
    ],
    \App\User::class => [
        'salt' => \App\User::class.'7623e9b0009feff8e024a689d6ef59ce',
        'length' => 5,
    ],

],

web.php

Route::get('/home/{user}', 'HomeController@index')->name('home');

trait hashidable.php

 <?php
namespace App\Http\Traits;
trait Hashidable
{
    public function getRouteKey()
    {
        return \Hashids::connection(get_called_class())->encode($this->getKey());
    }
}

用户.php

 use Hashidable;

RouteServiceProvider.php

 public function boot()
{
    parent::boot();

    
    Route::bind('user', function ($value, $route) {
        return $this->getModel(\App\User::class, $value);
    });
}
    // added by rap
    private function getModel($model, $routeKey)
{
    $id = \Hashids::connection($model)->decode($routeKey)[0] ?? null;
    $modelInstance = resolve($model);

    return  $modelInstance->findOrFail($id);
}

【问题讨论】:

  • 我的回答对你的问题有帮助吗,还是你还有问题?

标签: php laravel url


【解决方案1】:

这里有个误区,\Hashids::connection($model)不应该是模特。它应该是您在配置中定义的连接。

要使 hashids 工作,您需要创建配置。你可以像php artisan vendor:publish 那样创建你的配置文件。从那里看起来应该与此类似。

return [
    'default' => 'our_connection',

    'connections' => [
        'our_connection' => [
            'salt'   => 'your-salt',
            'length' => 6,
            'alphabet' => 'RANDOM CHARACTERS',
        ],
   ],
];

现在您可以使用默认连接,或指定您要使用的连接。

\Hashids::connection('our_connection')

这里的一切都在documentation 中得到了很好的描述。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,这些是我的代码。

    我没有放入 app.php

    'Hashids' => Vinkla \ Hashids \ Facades \ Hashids :: class,
    

    我的文件 hashids.php

    'connections' => [
    
        \App\Models\User::class => [
            'salt' => \App\Models\User::class.'fe4898dfe2fb8422f6dc8f79146d214d',
            'length' => 10,
        ],
        \App\Models\Course::class => [
            'salt' => 
      \App\Models\Course::class.'6dc29bf420a127578beaacab45f41351',
            'length' => 8,
        ],
        \App\Models\Unit::class => [
            'salt' => \App\Models\Unit::class.'c91a52c63222c1080a104b21f3331a60',
            'length' => 12,
        ],
    
    ],
    

    我的 trait 文件和你的和我的 RouteServiceProvider.php 文件一样

    public function boot()
    {
        parent::boot();
    
        Route::bind('user', function ($value, $route) {
            return $this->getModel(User::class, $value);
        });
    
        Route::bind('unit', function ($value, $route) {
            return $this->getModel(Unit::class, $value);
        });
    
        Route::bind('course', function ($value, $route) {
            return $this->getModel(Course::class, $value);
        });
    }
    
    protected function getModel($model, $routeKey) {
        $id = \Hashids::connection($model)->decode($routeKey)[0] ?? null;
        $modelInstance = resolve($model);
        return $modelInstance->findOrFail($id);
    }
    

    解决方案就是运行这个命令 php artisan config:clear 我遇到了同样的问题,这解决了一切

    solved hashids not configured

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      相关资源
      最近更新 更多