首先,让我们定义一个规则:用户只能访问自己创建的文章。facades.Gate.Define("update-post",func(ctxcontext.Context,argumentsmap[string]any)*access.Response{user:=ctx.Value("user").(models.User)post:=arguments["post"].(models.Post)ifuser.ID==post.UserID{returnaccess.NewAllowResponse()}else{returnaccess.NewDenyResponse("error")}})
首先,让我们定义一个规则:用户只能访问自己创建的文章。facades.Gate.Define("update-post",func(ctxcontext.Context,argumentsmap[string]any)*access.Response{user:=ctx.Value("user").(models.User)post:=arguments["post"].(models.Post)ifuser.ID==post.UserID{returnaccess.NewAllowResponse()}else{returnaccess.NewDenyResponse("error")}})
验证用户输入的数据是我们开发中最常见的需求,Goravel提供三种验证姿势,个个简单好用!第一种:简单直接式根据表单内容直接校验:func(r*PostController)Store(ctxhttp.Context){validator,err:=ctx.Request().Validate(map[string]string{"title":"required|max_len:255","body":"required",})}第二种:自由定义式自定义验证数据:validator,err:=facades.Validation.Make(map[string]any{"name":"Gor
验证用户输入的数据是我们开发中最常见的需求,Goravel提供三种验证姿势,个个简单好用!第一种:简单直接式根据表单内容直接校验:func(r*PostController)Store(ctxhttp.Context){validator,err:=ctx.Request().Validate(map[string]string{"title":"required|max_len:255","body":"required",})}第二种:自由定义式自定义验证数据:validator,err:=facades.Validation.Make(map[string]any{"name":"Gor
关于GoravelGoravel是一个功能完备、具有良好扩展能力的Web应用程序框架。作为一个起始脚手架帮助Golang开发者快速构建自己的应用。框架风格与Laravel保持一致,让PHPer不用学习新的框架,也可以愉快的玩转Golang!ORM模块上新「模型关联」,好用的飞起,有码有真相!定义模型一个用户可以发布多篇文章typeUserstruct{orm.ModelNamestringPosts[]*Post}typePoststruct{orm.ModelUserIDuintNamestring}查找(同时预加载关联模型)varusermodels.Userfacades.Orm.Que
关于GoravelGoravel是一个功能完备、具有良好扩展能力的Web应用程序框架。作为一个起始脚手架帮助Golang开发者快速构建自己的应用。框架风格与Laravel保持一致,让PHPer不用学习新的框架,也可以愉快的玩转Golang!ORM模块上新「模型关联」,好用的飞起,有码有真相!定义模型一个用户可以发布多篇文章typeUserstruct{orm.ModelNamestringPosts[]*Post}typePoststruct{orm.ModelUserIDuintNamestring}查找(同时预加载关联模型)varusermodels.Userfacades.Orm.Que