我有一个有许多动作的控制器:
class SomeController extends AbstractController
{
public function indexAction()
{
$this->denyAccessUnlessGranted('ROLE_SUPERMANAGER');
[...]
}
public function someAjaxAction()
{
$this->denyAccessUnlessGranted('ROLE_SUPERMANAGER');
[...]
}
public function someOtherAjaxAction()
{
$this->denyAccessUnlessGranted('ROLE_SUPERMANAGER');
[...]
}
}
为什么我不能这样做,在控制器的所有动作中拒绝访问?
public function __construct()
{
$this->denyAccessUnlessGranted('ROLE_SUPERMANAGER');
}
我得到Call to a member function has() on null
在AbstractController.php:218:
if (!$this->container->has('security.authorization_checker')) {
除了security.yaml
中的规则之外,在控制器类中是否有任何方法可以做到这一点?
你可以像这样在类级别添加senoframeworkextramplle的isGranted注释:
use SensioBundleFrameworkExtraBundleConfigurationIsGranted;
/**
* @IsGranted("ROLE_SUPERMANAGER")
**/
class SomeController extends AbstractController
{
// Your actions without auth check in each
}
frameworkextrabundance根据Symfony是不推荐使用的,不鼓励使用。也可以对SymfonyComponentSecurityHttpAttribute isgrant做同样的事情。这是一篇帮助我的好文章https://nebkam.medium.com/symfony-deprecated-route-and-method-annotations-4d5e1d34556a