Laravel-编码在getRouteKey中不起作用-Hashids \ Hashids

问题描述

我正在尝试将递增的ID散列在URL中。我似乎无法正确编码,我同时遵循了thisthis教程,但没有帮助。我在控制器中尝试了getRouteKey()中的代码,它似乎正在完成工作并按预期对ID进行哈希处理。

有人看到我在做什么错吗?

public function getRouteKey()
{
    $key = $this->getKey();
    $hashids = new \hashids\hashids('MySecretSalt',5);
    return $hashids->encode($key);
}

更新 然后我按如下方式使用绑定/解码;

    Route::model('property',Property::class);
    Route::bind('property',function ($value,$route) {
        $hashids = new \hashids\hashids('MySecretSalt',5);
        return $hashids->decode(intval($value))[0];
    });

我的路线是;

Route::get(
    '/{property}/vvv/','PropertyController@property_dashboard'
)->name('property.dashboard');

解决方法

我猜测您使用的是JavaScript前端而不是刀片服务器,因此您无权使用Laravel路由功能。如果是这样,并且您正在使用类似Ziggy的方法来创建路由,则始终可以将哈希值传递到ziggy的route方法中。您可以在模型中创建一个hashid属性,然后返回前端。