我如何在 ApiResource 中使用 PUT 和 DELETE?
How can i use PUT and DELETE in ApiResource?
我在 API 平台中有一个用户实体,我想限制访问,使用角色,并且只允许 ROLE_ADMIN 访问某些端点例如 PUT /users/x 和 DELETE /users/x, ROLE_USER 将只能访问 GET。
问题是,POST 和 GET 角色限制有效,但是当我添加 PUT或 DELETE 它们不起作用,我遇到 Symfony 错误。
这是我的用户实体的一部分:
/**
* Utilisateur
*
* @ApiResource(
* attributes={"access_control"="is_granted('ROLE_USER')"},
* collectionOperations={
* "get"={"method"="get"},
* "post"={"method"="POST", "access_control"="is_granted('ROLE_ADMIN')", "access_control_message"="Only admins can post to this entity"},
* "delete"={"method"="DELETE", "access_control"="is_granted('ROLE_ADMIN')", "access_control_message"="Only admins can update this entity"}
* }
* )
* @ORM\Table(name="`user`")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface
{
/**
* @var integer $id The user ID
*
* @ORM\Id()
* @Assert\Uuid
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
当我通过 Postman 获取 PUT 时出错:
An exception has been thrown during the rendering of a template ("There is no builtin action for the collection DELETE operation. You need to define the controller yourself in . (which is being imported from "/srv/api/config/routes/api_platform.yaml"). Make sure there is a loader supporting the "api_platform" type.").
实际上是因为您试图将 DELETE
操作映射到默认情况下未定义的 collectionOperation
。您需要创建一个自定义控制器。
我认为这实际上是一个小错误,您试图将其映射到 itermOperation
。
我在 API 平台中有一个用户实体,我想限制访问,使用角色,并且只允许 ROLE_ADMIN 访问某些端点例如 PUT /users/x 和 DELETE /users/x, ROLE_USER 将只能访问 GET。 问题是,POST 和 GET 角色限制有效,但是当我添加 PUT或 DELETE 它们不起作用,我遇到 Symfony 错误。
这是我的用户实体的一部分:
/**
* Utilisateur
*
* @ApiResource(
* attributes={"access_control"="is_granted('ROLE_USER')"},
* collectionOperations={
* "get"={"method"="get"},
* "post"={"method"="POST", "access_control"="is_granted('ROLE_ADMIN')", "access_control_message"="Only admins can post to this entity"},
* "delete"={"method"="DELETE", "access_control"="is_granted('ROLE_ADMIN')", "access_control_message"="Only admins can update this entity"}
* }
* )
* @ORM\Table(name="`user`")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface
{
/**
* @var integer $id The user ID
*
* @ORM\Id()
* @Assert\Uuid
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
当我通过 Postman 获取 PUT 时出错:
An exception has been thrown during the rendering of a template ("There is no builtin action for the collection DELETE operation. You need to define the controller yourself in . (which is being imported from "/srv/api/config/routes/api_platform.yaml"). Make sure there is a loader supporting the "api_platform" type.").
实际上是因为您试图将 DELETE
操作映射到默认情况下未定义的 collectionOperation
。您需要创建一个自定义控制器。
我认为这实际上是一个小错误,您试图将其映射到 itermOperation
。