Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 symfony2 中,每个用户创建的控制器都扩展了控制器类,如下所示,
class MyController extends Controller {
因此与会话处理相关的功能可通过 $this 对象获得,但 Vendor 和 Core 中的控制器不扩展 Controller 类,因此不提供对会话相关功能的访问。那么有什么方法可以在不扩展 Controller 类的情况下使用这些功能。
目前我正在使用$_SESSION[], 来设置和获取会话变量。
$_SESSION[]
除了上面还有什么办法。
Symfony2 为会话提供服务,这就是您要检索的内容。symfony2 中的所有服务都是使用服务容器检索的,这就是您所指的
$this->get('session');
要在您自己的控制器中正确使用服务容器,您可以...
get()
第一个选项是正确的方法,您可以完全控制哪些服务被注入到各自的控制器中(请参阅服务容器文档)