散列在Laravel 7中的路线 [英] Route Hashed in Laravel 7

查看:57
本文介绍了散列在Laravel 7中的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在laravel中缓存我的网址,该怎么办?我希望ID不可见,并替换为随机表达式

I want to hasher my urls in laravel, how do i do that? I want the ids to be invisible and replace with random expressions

例如:http//:laravel/update/id = http//:laravel/update/hvcd5213fc51dsssd£€#

exemple: http//:laravel/update/id = http//:laravel/update/hvcd5213fc51dsssd£€#

推荐答案

根据laravel文档

According to laravel doc https://laravel.com/docs/7.x/routing#explicit-binding => Customizing The Resolution Logic, you can customize your route logic.

无论如何传递散列值,您都必须在从数据库检索信息时调用散列方法(此处为md5).

However you have passed hash value you have to call hash method(here it is md5) while retrieving information from database.

Route::bind('user', function ($value) {
        return App\Models\User::where(DB::raw(md5('name')), $value)->firstOrFail();
    });

如果您不想像这样声明路由,也可以根据laravel文档重写遍历resolveRouteBinding方法.

If you do not want to declare route like these, alternatively you can override resolveRouteBinding method as per the laravel docs.

注意:此处md5是mysql哈希函数,如果您不从数据库中检索,则可以对代码中的值进行哈希处理并将其与URL值本身进行比较.

Note: Here md5 is mysql hash function, if you are not retrieving from database you can hash your value in code and compare it with URL value itself.

这篇关于散列在Laravel 7中的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆