Skip to content

Commit fcd9a21

Browse files
authored
Merge pull request api-platform#886 from soyuka/disable-item-operation
Document disabling item operation
2 parents 4feb408 + 3f13fa0 commit fcd9a21

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

core/operations.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ will be automatically added.
6363
<?php
6464
// api/src/Entity/Book.php
6565

66+
namespace App\Entity;
67+
6668
use ApiPlatform\Core\Annotation\ApiResource;
6769

6870
/**
@@ -84,6 +86,8 @@ The previous example can also be written with an explicit method definition:
8486
<?php
8587
// api/src/Entity/Book.php
8688

89+
namespace App\Entity;
90+
8791
use ApiPlatform\Core\Annotation\ApiResource;
8892

8993
/**
@@ -134,6 +138,36 @@ Or the XML configuration format:
134138
API Platform Core is smart enough to automatically register the applicable Symfony route referencing a built-in CRUD action
135139
just by specifying the method name as key, or by checking the explicitly configured HTTP method.
136140

141+
If you do not want to allow access to the resource item (i.e. you don't want a `GET` item operation), instead of omitting it altogether, you should instead declare a `GET` item operation which returns HTTP 404 (Not Found), so that the resource item can still be identified by an IRI. For example:
142+
143+
```
144+
<?php
145+
// api/src/Entity/Book.php
146+
147+
namespace App\Entity;
148+
149+
use ApiPlatform\Core\Action\NotFoundAction;
150+
use ApiPlatform\Core\Annotation\ApiResource;
151+
152+
/**
153+
* @ApiResource(
154+
* collectionOperations={
155+
* "get",
156+
* },
157+
* itemOperations={
158+
* "get"={
159+
* "controller"=NotFoundAction::class,
160+
* "read"=false,
161+
* "output"=false,
162+
* },
163+
* },
164+
* )
165+
*/
166+
class Book
167+
{
168+
}
169+
```
170+
137171
## Configuring Operations
138172

139173
The URL, the method and the default status code (among other options) can be configured per operation.
@@ -145,6 +179,8 @@ In addition to that, we require the `id` parameter in the URL of the `GET` opera
145179
<?php
146180
// api/src/Entity/Book.php
147181

182+
namespace App\Entity;
183+
148184
use ApiPlatform\Core\Annotation\ApiResource;
149185

150186
/**
@@ -245,6 +281,8 @@ you don't need to override all the operations to set the path but configure the
245281
<?php
246282
// api/src/Entity/Book.php
247283

284+
namespace App\Entity;
285+
248286
use ApiPlatform\Core\Annotation\ApiResource;
249287

250288
/**

0 commit comments

Comments
 (0)