gpt4 book ai didi

symfony - 如何使用带有字符串作为主题的 IsGranted 注释?

转载 作者:行者123 更新时间:2023-12-04 01:59:34 28 4
gpt4 key购买 nike

我发现在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com