Consuming
To consume request body you need a consumer - class that implements Articus\PathHandler\Consumer\ConsumerInterface
and is registered in configuration:
#Add entry in consumer plugin manager
Articus\PathHandler\Consumer\Factory\PluginManager:
invokables:
MyConsumer: My\Consumer
Library provides two consumers out of the box:
To use consumer for operation in your handler you just need to annotate operation method:
namespace My;
use Articus\PathHandler\Middleware;
use Articus\PathHandler\PhpAttribute as PHA;
use Psr\Http\Message\ServerRequestInterface;
#[PHA\Route("/entity")]
class Handler
{
#[PHA\Post()]
#[PHA\Consumer("*/*", "Json")]
public function handlePost(ServerRequestInterface $request)
{
$data = $request->getAttribute(Middleware::PARSED_BODY_ATTR_NAME)l
}
}
Each operation method can have several consumers. Just specify media range to determine when each of them should be called according request content type:
namespace My;
use Articus\PathHandler\Middleware;
use Articus\PathHandler\PhpAttribute as PHA;
use Psr\Http\Message\ServerRequestInterface;
#[PHA\Route("/entity")]
class Handler
{
#[PHA\Post()]
#[PHA\Consumer("application/json", "Json")]
#[PHA\Consumer("multipart/form-data", "Internal")]
public function handlePost(ServerRequestInterface $request)
{
$data = $request->getAttribute(Middleware::PARSED_BODY_ATTR_NAME)l
}
}
It is recommended to always specify mediaRange
to enforce anyone calling your API to supply a valid content type. If all operations in your handler need same consumer you can just annotate handler class insteadof annotating each method:
namespace My;
use Articus\PathHandler\Middleware;
use Articus\PathHandler\PhpAttribute as PHA;
use Psr\Http\Message\ServerRequestInterface;
#[PHA\Route("/entity")]
#[PHA\Consumer("application/json", "Json")]
class Handler
{
#[PHA\Post()]
public function handlePost(ServerRequestInterface $request)
{
$data = $request->getAttribute(Middleware::PARSED_BODY_ATTR_NAME)l
}
#[PHA\Patch()]
public function handlePatch(ServerRequestInterface $request)
{
$data = $request->getAttribute(Middleware::PARSED_BODY_ATTR_NAME)l
}
}
Last modified: 24 March 2024