You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR was merged into the 7.1 branch.
Discussion
----------
[HttpKernel] Introduce `#[MapUploadedFile]` controller argument attribute
| Q | A
| ------------- | ---
| Branch? | 7.1
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | #52678, #49138
| License | MIT
| Doc PR | -
## Usage Example
```php
# src/Controller/UserPictureController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;
#[AsController]
final class UserPictureController
{
#[Route('/user/picture', methods: ['PUT'])]
public function __invoke(
#[MapUploadedFile(
new Assert\File(mimeTypes: ['image/png', 'image/jpeg']),
)] ?UploadedFile $picture,
): Response {
return new Response('Your picture was updated');
}
}
```
```php
# src/Controller/UserDocumentsController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;
final class UserDocumentsController
{
#[Route('/user/documents', methods: ['PUT'])]
public function __invoke(
#[MapUploadedFile(
new Assert\File(mimeTypes: ['application/pdf'])
)] array $documents
): Response {
return new Response('Thanks for sharing your documents');
}
}
```
```php
# src/Controller/UserDocumentsController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;
final class UserDocumentsController
{
#[Route('/user/documents', methods: ['PUT'])]
public function __invoke(
#[MapUploadedFile(
new Assert\File(mimeTypes: ['application/pdf'])
)] UploadedFile ...$documents
): Response {
return new Response('Thanks for sharing your documents');
}
}
```
Commits
-------
b85dbd0 [HttpKernel] Add MapUploadedFile attribute
0 commit comments