我正在使用@Security
注释来控制哪些角色可以访问我的Symfony 3.4应用程序中的某些路由,当我登录时它可以工作,但是当user
对象不存在时(例如会话超时时)我抛出了以下异常。
Unable to get a property on a non-object.
供应商/symfony/symfony/src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php:78在Symfony \ Component \ ExpressionLanguage \ ExpressionLanguage-> evaluate('\'ROLE_MANAGER \'在user.getRoles()',
我的方法定义如下:
/**
* @Route("/club/{id}/trophies", name="club_trophies", methods={"GET","POST"})
* @IsGranted("IS_AUTHENTICATED_FULLY")
* @Security("'ROLE_MANAGER' in user.getRoles()")
* @param Club $club
* @return Response
*/
public function trophies(Club $club): Response
{
是否有使用Symfony表达语言或类似方法的方式,可以检查user
是否存在。还是有更好的方法?
使用注解检查用户是否具有给定角色的正确方法是:
@IsGranted("ROLE_MANAGER")
或:
@Security("is_granted('ROLE_MANAGER')")
您可以在这里看到更多:https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/security.html