Attributes
The core feature of this bundle, the below attributes mainly exist to facilitate handling requests and responses.
RequestBody
RequestBodyThis attribute is one with the most features in it. It decodes the request message body and passes it to a method argument in a controller. It supports scalars, objects, collections, and file-based method arguments.
Scalars
For scalar method arguments the ConverterManager is used if necessary.
namespace App\Controller;
use Jungi\FrameworkExtraBundle\Attribute\RequestBody;
use Symfony\Component\Routing\Attribute\Route;
class MessageController
{
#[Route('/messages', methods: ['POST'])]
public function sendMessage(#[RequestBody] string $message)
{
// ..
}
}Objects
On the contrary to the scalar types, it uses the MessageBodyMapperManager to decode the request message body.
Collections
Unfortunately, at the current state, PHP does not support declaring a collection with the desired type. In order to achieve that, the type parameter has been provided. It accepts types of well-known syntax type[].
Files
When the request message body contains a file, you can declare your method argument as UploadedFile, File, SplFileInfo or SplFileObject. Thanks to the TmpFileUtils you can query those objects for any public method like getFilename(), getPathname() and so on.
UploadedFile
It's a bit a special one, it covers all public methods like getClientOriginalName() or getClientMimeType().
The getClientOriginalName() uses the Content-Disposition header. In general, this header is used in responses and in multipart/form-data requests. For this to work, the Content-Disposition must be set to inline, and the filename parameter must be provided.
On the contrary, the getClientMimeType() uses the Content-Type header.
RequestParam
RequestParamIt binds a request parameter to a method argument in a controller. It supports multipart/form-data requests and that means you can even access a file through this attribute.
QueryParam
QueryParamIt binds a query parameter to a method argument in a controller.
QueryParams
QueryParamsIt is similar to the @QueryParam, but instead of a single query parameter, it takes all query parameters to pass them as a single method argument in a controller. This attribute requires an argument to be of an object type.
RequestCookie
RequestCookieIt binds a cookie value to a method argument in a controller.
RequestHeader
RequestHeaderIt binds a header value to a method argument in a controller.
As you can see in the example when a method argument is declared as array it will pass all values of the header to it.
Last updated
Was this helpful?