【问题标题】:how to hide id from URL laravel 7?如何从 URL laravel 7 中隐藏 id?
【发布时间】:2020-05-21 02:18:02
【问题描述】:

我有这个网址:

http://127.0.0.1:8000/deliverer/4

我想要这样的

http://127.0.0.1:8000/deliverer/

这是我的功能:

    public function show($id)
    {

//        $userhash->hashids->encode($id);
        $user=User::find($id);

        return view('deliverer.profile')->with('user',$user);
    }

这是我的路线

Route::get('deliverer/{id}', 'deliverer\DelivererController@show')->name('profile');

这个在视图中

<a          href="http://127.0.0.1:8000/deliverer/{{ Auth::user()->id }}" >
                    <i class="nc-icon nc-single-02"></i>
                    <p>Profile</p>
                </a>

【问题讨论】:

    标签: laravel laravel-7


    【解决方案1】:

    假设您要完成的是查看当前经过身份验证的用户的个人资料,那么您应该按照以下步骤操作:

    首先您必须修改路由并从 URL 中删除 {id},如下所示:

    Route::get('deliverer', 'deliverer\DelivererController@show')->name('profile');
    

    然后,在控制器内部,您必须从 show() 方法中删除 $id 参数并更改方法以从经过身份验证的用户那里获取 id。

    public function show($id)
    {
    //        $userhash->hashids->encode($id);
        $user = \Auth::user();
    
        return view('deliverer.profile')->with('user',$user);
    }
    

    当然,您必须从视图路由中删除 Auth::user()->id(),并且可能使用命名路由而不是硬编码它,如下所示:

    <a href="{{ route('profile') }}">
        <i class="nc-icon nc-single-02"></i>
        <p>Profile</p>
    </a>
    

    【讨论】:

      【解决方案2】:

      我假设您只是想隐藏 id,以便用户可以猜测下一个数字或尝试提取他们不应该的记录。

      你研究过 UUID 吗?此处的示例:(https://dev.to/wilburpowery/easily-use-uuids-in-laravel-45be) 这可能是一个解决方案。

      此外,如果您担心有人可能会篡改 URL 以提取记录,您应该考虑保护您的模型。检查用户是否应该有权访问该特定记录。实现这一目标的方法有很多。

      【讨论】:

      • 使用 Ramsey uuid,创建列 uuid,从 uuid4 Ramsey 函数插入 uuid,而不是在 url 路由中用 uuid 替换 Id,在函数路由中使用 where close 而不是 find。
      【解决方案3】:

      您可以使用令牌或配置中间件,或者您可以像以前一样对 id 进行哈希或加密,并在调用后进行验证

      网址是这样的:

      http://127.0.0.1:8000/deliverer/?id=aze45a8sd54q
      

      【讨论】:

        猜你喜欢
        • 2021-01-06
        • 2015-04-12
        • 2019-01-11
        • 2012-08-15
        • 2021-10-31
        • 1970-01-01
        • 2018-07-08
        • 2017-01-19
        • 1970-01-01
        相关资源
        最近更新 更多