Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在不发送公共请求中的id的情况下获取模型数据的最佳方法是什么?_Php_Laravel - Fatal编程技术网

Php 在不发送公共请求中的id的情况下获取模型数据的最佳方法是什么?

Php 在不发送公共请求中的id的情况下获取模型数据的最佳方法是什么?,php,laravel,Php,Laravel,我正在拉维尔建立在线课程网站。我想在公共页面中显示教师信息,我不想在公共请求中显示id。我应该使用UUID还是加密 例如: teacher/1/courses teacher/1/info 我认为uuid是最好的方法,它的可读性相当好,它不是加密安全的,但破坏它是非常不实际的 使用模型绑定很容易实现。想象一条有这个控制器的路线 class CourseController { public function index(Teacher $teacher) { ...

我正在拉维尔建立在线课程网站。我想在公共页面中显示教师信息,我不想在公共请求中显示id。我应该使用UUID还是加密

例如:

teacher/1/courses
teacher/1/info


我认为uuid是最好的方法,它的可读性相当好,它不是加密安全的,但破坏它是非常不实际的

使用模型绑定很容易实现。想象一条有这个控制器的路线

class CourseController {
    public function index(Teacher $teacher) {
        ...
    }
}

Route::get('teacher/{teacher}/courses', 'CourseController@index');
您需要模型上的uuid列,请为此进行迁移。否则将覆盖如何在
Teacher.php
模型上解析模型绑定

class Teacher {
    public function getRouteKey(): string
    {
        return $this->uuid;
    }

    public function resolveRouteBinding($value, $field = null)
    {
        return self::where('uuid', $value)->firstOrFail();
    }
}
从这里开始,您只需在创建教师模型时设置uuid,然后挂接到创建事件中,就可以了

class Teacher {
    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->uuid = resolve(UuidFactory::class)->uuid4();
        });
}

uuid更好,但您也可以使用laravel内置的加密-解密方法,它会使您的url变长。我使用的是加密方法。你能帮我解决这个php问题吗?请好久不见了。