0
大家好,thansk爲您的時間。致電security.authorization_checker服務
我與Symfony 3.0.2
和類我試圖獲取服務security.authorization_checker才能看到誰連接到我的應用程序 的ROLE_工作,但我有此錯誤:
未遂調用類「AppBundle \ Form \ RegistrationType」的名爲「get」的未定義方法。 您是否打電話給「getBlockPrefix」,「getName」或「getParent」?
class RegistrationType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options){
$roles = new LoadRoles();
// $container = new Container();
// $this->denyAccessUnlessGranted('ROLE_ADMIN');
//LOAD ROLES FOR SUPER ADMIN
if($this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')){
$roles->getRolesForAdmin($builder, $options);
}
//LOAD ROLES FOR ADMIN
if($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')){
$roles->getRolesForManager($builder, $options);
}
//LOAD ROLES FOR ASSOCIATION
if($this->get('security.authorization_checker')->isGranted('ROLE_ASSOCIATION')){
$roles->getRolesForAssociation($builder, $options);
}
}
的probblem位於:$this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')
功能無法識別的get()函數,因爲它不擴展控制器 我不知道是否有訪問該服務的方法嗎?我是新與Symfony的,請原諒我的 誠懇
你不是試圖在你的*控制器裏面調用它,你試圖在你內部調用它r * FormType * - 這是一個區別。 FormType通常無權訪問服務容器。也許最好是編輯問題並告訴我們你想要達到的目標。 – ccKep
@ccKep感謝您的回覆,我將編輯我的帖子 – Ahmed
閱讀將您的表單類型定義爲服務並注入授權檢查器:http://symfony.com/doc/current/form/form_dependencies.html#define -your-form-as-a-service您也可以通過授權檢查器作爲選項。 – Cerad