【发布时间】: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);
}
【问题讨论】:
-
我的回答对你的问题有帮助吗,还是你还有问题?