我发现在 Symfony 中我可以使用注释以一种干净的方式向我的 Controller 方法添加额外的功能,这真的很方便。例如:
/**
* @Route("/{id}")
* @IsGranted("view", subject="product")
* @return Response
*/
public function view(Product $product)
{
dump(compact('product'));
return new Response('It worked!');
}
但是,对于创建方法,我没有产品实例,所以我想使用 @IsGranted
注释作为主题字符串“App\Entity\Post” .我希望我能这样做:
/**
* @Route("/")
* @IsGranted("create", subject=Product::class)
* @return Response
*/
public function create()
{
return new Response('Did it work?');
}
但不幸的是,我收到以下错误:找不到 @IsGranted 注释的主题“App\Entity\Product”。尝试向您的 Controller 方法添加“$App\Entity\Product”参数。
所以 @IsGranted
仍然认为它应该寻找名称为 $App\Entity\Product
的方法参数。有没有一种方法可以仅将其与字符串文字一起使用?
最佳答案
你不能省略主题属性吗?
我没有使用注释,但我知道 Symfony 身份验证检查器允许在没有主题的情况下调用“isGranted”。
请参阅此处示例:https://symfony.com/doc/current/security.html#securing-controllers-and-other-code
关于symfony - 如何使用带有字符串作为主题的 IsGranted 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48266367/