Accessing user session from a custom routing class

此生再无相见时 提交于 2019-12-08 01:15:53

问题


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

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.

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

Thanks!


回答1:


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.

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.




回答2:


A workaround I have implemented (for now), is getting some data from somewhere (not ideal, I'm willing to access the user session yet), and set a new parameter inside $params, in the generate method of the custom routing class.

Hope it helps...



来源:https://stackoverflow.com/questions/2455817/accessing-user-session-from-a-custom-routing-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!