Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #49978 [HttpKernel] Introduce
#[MapUploadedFile]
controller…
… argument attribute (renedelima) 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
- Loading branch information
Showing
6 changed files
with
408 additions
and
11 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/HttpKernel/Attribute/MapUploadedFile.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Attribute; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestPayloadValueResolver; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
#[\Attribute(\Attribute::TARGET_PARAMETER)] | ||
class MapUploadedFile extends ValueResolver | ||
{ | ||
public ArgumentMetadata $metadata; | ||
|
||
public function __construct( | ||
/** @var Constraint|array<Constraint>|null */ | ||
public Constraint|array|null $constraints = null, | ||
public ?string $name = null, | ||
string $resolver = RequestPayloadValueResolver::class, | ||
public readonly int $validationFailedStatusCode = Response::HTTP_UNPROCESSABLE_ENTITY, | ||
) { | ||
parent::__construct($resolver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.