|
| 1 | +# Using Parameters |
| 2 | + |
| 3 | +API Platform provides a powerful and flexible parameter system that allows you to control various aspects of your API, from filtering and pagination to serialization and security. This guide will walk you through the different types of parameters available and how to use them effectively. |
| 4 | + |
| 5 | +## Query Parameters |
| 6 | + |
| 7 | +Query parameters are the most common type of parameter. They are passed in the URL's query string (e.g., `?page=2`). You can define query parameters on a resource or on a specific operation using the `#[QueryParameter]` attribute. |
| 8 | + |
| 9 | +Here's an example of how to define a query parameter for pagination: |
| 10 | + |
| 11 | +```php |
| 12 | +<?php |
| 13 | +// api/src/ApiResource/Book.php |
| 14 | + |
| 15 | +namespace App\ApiResource; |
| 16 | + |
| 17 | +use ApiPlatform\Metadata\ApiResource; |
| 18 | +use ApiPlatform\Metadata\GetCollection; |
| 19 | +use ApiPlatform\Metadata\QueryParameter; |
| 20 | + |
| 21 | +#[ApiResource( |
| 22 | + operations: [ |
| 23 | + new GetCollection( |
| 24 | + parameters: [ |
| 25 | + 'page' => new QueryParameter(description: 'The page number.', schema: ['type' => 'integer', 'default' => 1]) |
| 26 | + ] |
| 27 | + ) |
| 28 | + ] |
| 29 | +)] |
| 30 | +class Book |
| 31 | +{ |
| 32 | + // ... |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +In this example, we've defined a `page` query parameter for the `GetCollection` operation. The parameter has a description, a schema that specifies its type and default value. |
| 37 | + |
| 38 | +## Header Parameters |
| 39 | + |
| 40 | +Header parameters are passed in the request headers (e.g., `Authorization: Bearer <token>`). You can define header parameters using the `#[HeaderParameter]` attribute. |
| 41 | + |
| 42 | +Here's an example of how to define a header parameter for an API key: |
| 43 | + |
| 44 | +```php |
| 45 | +<?php |
| 46 | +// api/src/ApiResource/Book.php |
| 47 | + |
| 48 | +namespace App\ApiResource; |
| 49 | + |
| 50 | +use ApiPlatform\Metadata\ApiResource; |
| 51 | +use ApiPlatform\Metadata\Get; |
| 52 | +use ApiPlatform\Metadata\HeaderParameter; |
| 53 | + |
| 54 | +#[ApiResource( |
| 55 | + operations: [ |
| 56 | + new Get( |
| 57 | + parameters: [ |
| 58 | + 'API_KEY' => new HeaderParameter(description: 'Your API key.', required: true, schema: ['type' => 'string']) |
| 59 | + ] |
| 60 | + ) |
| 61 | + ] |
| 62 | +)] |
| 63 | +class Book |
| 64 | +{ |
| 65 | + // ... |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +In this example, we've defined an `API_KEY` header parameter for the `Get` operation. The parameter is required and has a schema that specifies its type. |
| 70 | + |
| 71 | +## Path Parameters as Links |
| 72 | + |
| 73 | +Path parameters are the dynamic parts of the URL (e.g., the `{id}` in `/books/{id}`). In API Platform, path parameters are represented by `ApiPlatform\Metadata\Link` objects. These objects allow you to define the relationship between the path parameter and the resource being requested. |
| 74 | + |
| 75 | +This is particularly useful for fetching related resources and for filtering collections based on path parameters. |
| 76 | + |
| 77 | +Here's an example of how to use a path parameter to fetch a collection of books by a specific author: |
| 78 | + |
| 79 | +```php |
| 80 | +<?php |
| 81 | +// api/src/ApiResource/Book.php |
| 82 | + |
| 83 | +namespace App\ApiResource; |
| 84 | + |
| 85 | +use ApiPlatform\Metadata\ApiResource; |
| 86 | +use ApiPlatform\Metadata\GetCollection; |
| 87 | +use ApiPlatform\Metadata\Link; |
| 88 | +use App\Entity\Author; |
| 89 | + |
| 90 | +#[ApiResource( |
| 91 | + operations: [ |
| 92 | + new GetCollection( |
| 93 | + uriTemplate: '/authors/{authorId}/books', |
| 94 | + uriVariables: [ |
| 95 | + 'authorId' => new Link(toProperty: 'author', fromClass: Author::class) |
| 96 | + ] |
| 97 | + ) |
| 98 | + ] |
| 99 | +)] |
| 100 | +class Book |
| 101 | +{ |
| 102 | + public int $id; |
| 103 | + public string $title; |
| 104 | + public Author $author; |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +In this example, the `authorId` path parameter is linked to the `author` property of the `Book` resource. When a request is made to `/authors/1/books`, API Platform will automatically filter the `Book` collection to return only the books where the `author` property is the `Author` with an ID of 1. |
| 109 | + |
| 110 | +This filtering is made possible by the `ApiPlatform\Doctrine\Orm\Extension\ParameterExtension`. This extension inspects the `Link` objects defined for an operation and, if a `toProperty` is specified, it automatically adds a `WHERE` clause to the Doctrine query to filter the results. |
| 111 | + |
| 112 | +## Parameter Validation |
| 113 | + |
| 114 | +API Platform automatically validates parameters based on their schema definition. For example, if you define a parameter with `['type' => 'integer']`, API Platform will ensure that the value passed in the request is a valid integer. |
| 115 | + |
| 116 | +You can also use [Symfony's validation constraints](https://symfony.com/doc/current/validation.html) to define more complex validation rules. |
| 117 | + |
| 118 | +Here's an example of how to use validation constraints on a parameter: |
| 119 | + |
| 120 | +```php |
| 121 | +<?php |
| 122 | +// api/src/ApiResource/Book.php |
| 123 | + |
| 124 | +namespace App\ApiResource; |
| 125 | + |
| 126 | +use ApiPlatform\Metadata\ApiResource; |
| 127 | +use ApiPlatform\Metadata\Get; |
| 128 | +use ApiPlatform\Metadata\QueryParameter; |
| 129 | +use Symfony\Component\Validator\Constraints as Assert; |
| 130 | + |
| 131 | +#[ApiResource( |
| 132 | + operations: [ |
| 133 | + new Get( |
| 134 | + parameters: [ |
| 135 | + 'rating' => new QueryParameter( |
| 136 | + description: 'The rating of the book.', |
| 137 | + schema: ['type' => 'integer'], |
| 138 | + constraints: [ |
| 139 | + new Assert\Range(min: 1, max: 5) |
| 140 | + ] |
| 141 | + ) |
| 142 | + ] |
| 143 | + ) |
| 144 | + ] |
| 145 | +)] |
| 146 | +class Book |
| 147 | +{ |
| 148 | + // ... |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +In this example, we've added a `Range` constraint to the `rating` parameter, ensuring that its value is between 1 and 5. |
| 153 | + |
| 154 | +## Parameter Security |
| 155 | + |
| 156 | +You can secure your parameters using the `security` option. This allows you to restrict access to parameters based on the user's roles or other conditions. |
| 157 | + |
| 158 | +Here's an example of how to secure a parameter: |
| 159 | + |
| 160 | +```php |
| 161 | +<?php |
| 162 | +// api/src/ApiResource/Book.php |
| 163 | + |
| 164 | +namespace App\ApiResource; |
| 165 | + |
| 166 | +use ApiPlatform\Metadata\ApiResource; |
| 167 | +use ApiPlatform\Metadata\Get; |
| 168 | +use ApiPlatform\Metadata\QueryParameter; |
| 169 | + |
| 170 | +#[ApiResource( |
| 171 | + operations: [ |
| 172 | + new Get( |
| 173 | + parameters: [ |
| 174 | + 'show_unpublished' => new QueryParameter( |
| 175 | + description: 'Whether to show unpublished books.', |
| 176 | + schema: ['type' => 'boolean'], |
| 177 | + security: "is_granted('ROLE_ADMIN')" |
| 178 | + ) |
| 179 | + ] |
| 180 | + ) |
| 181 | + ] |
| 182 | +)] |
| 183 | +class Book |
| 184 | +{ |
| 185 | + // ... |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +In this example, the `show_unpublished` parameter can only be used by users with the `ROLE_ADMIN` role. |
| 190 | + |
| 191 | +## Custom Parameter Providers |
| 192 | + |
| 193 | +For more advanced use cases, you can create your own custom parameter providers. A parameter provider is a class that implements the `ApiPlatform\State\ParameterProviderInterface`. This interface has a `provide` method that allows you to implement your own logic for handling a parameter. |
| 194 | + |
| 195 | +Here's an example of a custom parameter provider that modifies the serialization context based on a query parameter: |
| 196 | + |
| 197 | +```php |
| 198 | +<?php |
| 199 | +// src/Parameter/GroupsParameterProvider.php |
| 200 | + |
| 201 | +namespace App\Parameter; |
| 202 | + |
| 203 | +use ApiPlatform\State\ParameterProviderInterface; |
| 204 | +use ApiPlatform\Metadata\Parameter; |
| 205 | +use ApiPlatform\Metadata\HttpOperation; |
| 206 | + |
| 207 | +class GroupsParameterProvider implements ParameterProviderInterface |
| 208 | +{ |
| 209 | + public function provide(Parameter $parameter, array $uriVariables = [], array $context = []): HttpOperation |
| 210 | + { |
| 211 | + $request = $context['request']; |
| 212 | + return $context['operation']->withNormalizationContext(['groups' => $request->query->all('groups')]); |
| 213 | + } |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +You can then use this provider on a parameter: |
| 218 | + |
| 219 | +```php |
| 220 | +<?php |
| 221 | +// api/src/ApiResource/Book.php |
| 222 | + |
| 223 | +namespace App\ApiResource; |
| 224 | + |
| 225 | +use ApiPlatform\Metadata\ApiResource; |
| 226 | +use ApiPlatform\Metadata\Get; |
| 227 | +use ApiPlatform\Metadata\QueryParameter; |
| 228 | +use App\Parameter\GroupsParameterProvider; |
| 229 | + |
| 230 | +#[ApiResource( |
| 231 | + operations: [ |
| 232 | + new Get( |
| 233 | + parameters: [ |
| 234 | + 'groups' => new QueryParameter( |
| 235 | + description: 'The serialization groups to use.', |
| 236 | + provider: GroupsParameterProvider::class |
| 237 | + ) |
| 238 | + ] |
| 239 | + ) |
| 240 | + ] |
| 241 | +)] |
| 242 | +class Book |
| 243 | +{ |
| 244 | + // ... |
| 245 | +} |
| 246 | +``` |
| 247 | + |
| 248 | +In this example, when the `groups` parameter is present in the request, the `GroupsParameterProvider` will be invoked, and it will modify the serialization context to use the specified groups. |
0 commit comments