Php 使用Laravel 5.2中的vinkla/hashids包来删除id';在URL';s
我已经在laravel 5.2上安装并配置了最新版本(2.3.0)的vinkla/hashids 我不确定如何在我的URL路由上实现它的功能 我想删除URL路由中显示的所有id属性 例如-应该成为 我已尝试通过将以下方法添加到App\Profile.php来覆盖illumb\Database\elount\Model.php上的以下方法-Php 使用Laravel 5.2中的vinkla/hashids包来删除id';在URL';s,php,laravel-5.2,hashids,Php,Laravel 5.2,Hashids,我已经在laravel 5.2上安装并配置了最新版本(2.3.0)的vinkla/hashids 我不确定如何在我的URL路由上实现它的功能 我想删除URL路由中显示的所有id属性 例如-应该成为 我已尝试通过将以下方法添加到App\Profile.php来覆盖illumb\Database\elount\Model.php上的以下方法- public function getRouteKey() { dd('getRouteKey method'); return Hashids::e
public function getRouteKey()
{
dd('getRouteKey method');
return Hashids::encode($id);
}
我的dd没有显示,因此我没有正确覆盖它
请告诉我如何正确地实现此功能
谢谢以下是我为同样的问题所做的: 说你的路线有
Route::get('/profile/{profile}', 'ProfileController@showProfile');
然后在模型中:
// Attribute used for generating routes
public function getRouteKeyName()
{
return 'hashid';
}
// Since "hashid" attribute doesn't "really" exist in
// database, we generate it dynamically when requested
public function getHashidAttribute()
{
return Hashids::encode($this->id);
}
// For easy search by hashid
public function scopeHashid($query, $hashid)
{
return $query->where('id', Hashids::decode($hashid)[0]);
}
最后,需要绑定路由参数“profile”。您必须先解码它,然后在数据库中搜索(默认绑定将不起作用)。因此,在app/Providers/RouteServiceProvider.php中:
/**
* Define your route model bindings, pattern filters, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function boot(Router $router)
{
Route::bind('profile', function($hashid, $route) {
try {
return Profile::hashid($hashid)->first();
}
catch (Exception $e) {
abort(404);
}
});
parent::boot($router);
}
我不想使用显式绑定,我想知道如何在一些隐式绑定中做到这一点…@CaioKawasaki,这也需要在数据库中使用哈希字段。你想要吗?谢谢你的回答。不过,我相信更改“getRouteKeyName”值是没有必要的。对于未来的读者来说。我发现这真的很有帮助: