我想将加密的 ID 作为路由参数传递。目前,我正在使用 Route 模型绑定,它返回 404
问问题
23 次
1 回答
1
您可以为该模式创建一个自定义解析器,例如
use Illuminate\Support\Facades\Crypt;
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($encryptedId, $field = null)
{
return $this->where('id', Crypt::decryptString ($encryptedId))->firstOrFail();
}
在上面的代码中,您可以获得encrypted
数据,因为$encryptedId
您可以解密并用于查询以执行搜索
你可以做同样的事情RouteServiceProvider
use App\Models\User;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Crypt;
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
Route::bind('user', function ($encryptedId) {
return User::where('id', Crypt::decryptString ($encryptedId))->firstOrFail();
});
// ...
}
参考链接https://laravel.com/docs/9.x/routing#customizing-the-resolution-logic
于 2022-02-18T05:22:22.697 回答