从自定义路由类访问用户会话 [英] Accessing user session from a custom routing class

查看:42
本文介绍了从自定义路由类访问用户会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于从自定义路由类访问用户会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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