Symfony 2 で現在のユーザーにアクセスする方法について少し混乱しています。現在、現在のユーザーの ROLES に応じてフォーム (AbstractType) のバリエーションを表示しようとしています。
同様の質問がGremoによってすでに回答されています: EntityRepositoryで現在ログインしているユーザーにアクセスする
私の質問は: JMSDiExtraBundleを使用せずに、AbstractType クラス内のユーザーにアクセスする Symfony 2 ネイティブの方法はありますか? ありがとう!
これが私の現在のコードです:
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class Comment extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
//somehow access the current user here
$builder
->add('name')
->add('comment_text')
->add('comment_email')
// Add more fields depending on user role
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\Comment'
));
}
public function getName()
{
return 'acme_demobundle_comment';
}
}
編集:現在ログインしているユーザー(security.context)を探しています