Symfony2: Access the container in the repository
I'm trying to show an user oriented choice list in a form and I don't manage to access to the container to get the current User.
I don't see how to get it in the开发者_高级运维 Repository neither than in the Type.
Any Idea?
Let say you created a FormType
class. You don't know how to pass the container in this object.
Create now your own type extended from FormType and pass the container through the constructor
class MyType extends FormType
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
}
In your config.yml, define your new type
mytype:
class: ...\MyType
arguments: ["@service_container"]
tags:
- { name: form.type }
Now, use MyType instead of FormType in all your controllers
Perhaps you can request the User object in the controller, and pass it on to the repository in the constructor?
精彩评论