Php 如何在Laravel 5.2中加密url中的所有ID

Php 如何在Laravel 5.2中加密url中的所有ID,php,encryption,laravel-5,laravel-5.2,url-encoding,Php,Encryption,Laravel 5,Laravel 5.2,Url Encoding,我需要加密所有url ID,如: user/edit/1 items/edit/35 posts/details/52 到 在blade文件和controllers中,id使用了url('items/edit/2'),并且在controller中,一些函数通过对象传递,比如public function itemedit(items$items) 我尝试了$encrypt\u val=Crypt::encrypt($value)和$decrypt\u val=Crypt::decrypt($e

我需要加密所有url ID,如:

user/edit/1
items/edit/35
posts/details/52

blade
文件和
controllers
中,id使用了
url('items/edit/2')
,并且在controller中,一些函数通过对象传递,比如
public function itemedit(items$items)

我尝试了
$encrypt\u val=Crypt::encrypt($value)
$decrypt\u val=Crypt::decrypt($encrypt\u val)但我需要在任何地方都这样做


有什么捷径或中间件功能可以做到这一点吗?

您可以使用
Uuid
而不是使用integer
id
。为此,请按照说明进行操作:

只需创建一个
特征

trait Uuids
{
    /**
     * Boot function from laravel.
     */
    protected static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            $model->{$model->getKeyName()} = Uuid::generate()->string;
        });
    }
}
在您的模型中使用上述特征:

use SomeNamespcaeOfTrait;

class User extends Eloquent
 {
    use Uuids;

    /**
     * @var bool
     */
    public $incrementing = false;
}
在迁移中,使用
uuid
而不是
integer

使用Laravel哈希ID

您可以像下面这样对id进行编码

$encoded_id = Hashids::encode($id);
<url>/users/edit/sdfjk54dfds
您的URL如下所示

$encoded_id = Hashids::encode($id);
<url>/users/edit/sdfjk54dfds
/users/edit/sdfjk54dfds
哈希ID安装指南

有一个名为的包,可以根据需要运行。与sumit的答案中的答案类似,它是基于HashID构建的,但专门设计用于URL

使用上述软件包,您只需在控制器中添加一个trait和typehint:

类后扩展模型{
使用HasHashSlug;
}
//routes/web.php
路由::资源('/posts',PostController');
//app/Http/Controllers/PostController.php
公共活动展览(Post$Post){
返回视图('post.show',压缩视图('post');
}

谢谢您的回答,但我已经构建了数据库,因此我知道如何使用它。如果您仍在开发中,则只需按照上面的说明运行php artisan migrate:reset,然后运行php artisan migrate--stepMy应用程序已投入生产。因此,我无法重置迁移。