栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

获取登录的用户信息以显示

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取登录的用户信息以显示

您可以使用轻松

ctx.Values().Set/Get
使路由的处理程序或中间件之间共享某些内容。

// load session manager oncesess := connectSess()func authCheck(ctx context.Context) {    session := sess.Start(ctx)    // Load your user here.    // [...]    // Save the returning user to the local storage of this handlers chain, once.     ctx.Values().Set("user", user) // <-- importANT}app.Get("/", func(ctx context.Context) {    // Get the user from our handlers chain's local storage.    user := ctx.Values().Get("user") // <-- importANT    // Bind the "{{.user}}" to the user instance.    ctx.ViewData("user", user)    // Render the template file.    ctx.View("index.html")})app.Get("dashboard", func(ctx context.Context) {    // The same, get the user from the local storage...    user := ctx.Values().Get("user") // <-- importANT    ctx.ViewData("user", user)    ctx.View("index.html")})

就这样,很简单,对吧?

但我有一些 笔记供您参考 ,如果您有更多时间请阅读。

当您位于根目录“
/”上时,不必

.Party
为了添加中间件(begin(
Use
)或finish(
Done
))而为它()创建一个参与者,只需使用
iris.Application
实例
app.Use/Done

不要这样写:

allRoutes := app.Party("/", logThisMiddleware, authCheck) {    allRoutes.Get("/", myHandler)}

改为:

app.Use(logThisMiddleware, authCheck)app.Get("/", myHandler)

更容易 阅读和理解


注意到,

;
在功能的最后使用的是您的编辑器和
gopre
工具,它们会删除它们,当您使用Go编程语言编写程序时,您不应该这样做,而是全部删除
;

最后,请阅读文档和示例,我们在https://github.com/kataras/iris/tree/master/_examples中提供了许多示例,希望您一切顺利!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372000.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号