如何在模型中获取 Symfony 会话变量? [英] How to get Symfony session variable in model?
本文介绍了如何在模型中获取 Symfony 会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不使用 sfContext::getInstance()
的情况下在 symfony 模型中传递会话变量?
How can I pass session variable in symfony model without using sfContext::getInstance()
?
推荐答案
推荐的方式称为依赖注入,其工作方式如下:您在模型文件中创建一个 setUser()
方法,即将给定的参数保存到私有属性:
The recommended way is called dependency injection, and works like this: you create a setUser()
method in your model file, that saves the given parameter to a private property:
class Foo {
private $_user;
public function setUser(myUser $user) {
$this->_user = $user;
}
// ... later:
public function save(Doctrine_Connection $conn = null) {
// use $this->_user to whatever you need
}
}
这看起来很笨拙,因为它是.但是如果你不回答这个问题你想做什么?我不能给出替代方案.
This looks clumsy, because it is. But without you answering the question what are you trying to do? I cannot give an alternative.
推荐文章:
- What is Dependency Injection? - a post series on Fabien Potencier's blog
- Dependency Injection - the design patter in detail on wikipedia
这篇关于如何在模型中获取 Symfony 会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文