从自定义路由类访问用户会话

编程入门 行业动态 更新时间:2024-10-12 01:26:13
本文介绍了从自定义路由类访问用户会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法从自定义路由类访问用户对象?

Is there some way to acces the user object from a custom routing class?

我想在生成url时添加一个参数,该参数在用户会话中,所以我需要访问它.

I'd like to add a parameter when generating a url, and that parameter is inside the user session, so I need to access it.

我发现访问的唯一方法是使用 sfContext::getInstance()->getUser(),但众所周知它效率低下.

The only way I found to access is using the sfContext::getInstance()->getUser(), but it's known to be inefficient.

谢谢!

推荐答案

我会按照你提到的方式来写 - 我在类似的情况下使用过这种方法并且从来没有遇到过性能方面的问题,我怀疑你会是一样.

I'd write it the way you mention - I've used that method in similar situations and never had an issue performance wise, and suspect you will be the same.

另外,从来没有听说过这被认为是低效的,但它有点不受欢迎,因为它将路由与上下文结合起来.解决这个问题的另一种方法是将变量传递给路由,就像传递任何其他参数一样(如果需要整个参数,也可以传递给用户对象).如果你需要这样做很多,你总是可以创建一个自定义的 url 帮助器来包装现有的 url_for 方法,将此参数添加到任何其他传递的细节中.

Also, never heard this mentioned as inefficient, but it is a little bit frowned upon because it couples the route to the context. An alternative that would overcome this would be to pass the variable to the route as you would any other parameter (or the user object if you need the whole thing). If you need to do this a lot, you can always make a custom url helper that wraps the existing url_for method, adding this param to any other details passed.

更多推荐

从自定义路由类访问用户会话

本文发布于:2023-11-27 20:03:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   路由   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!