PHP code example of articus / path-handler
1. Go to this page and download the library: Download articus/path-handler library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
articus / path-handler example snippets
namespace My;
use Articus\PathHandler\PhpAttribute as PHA;
use Articus\PathHandler\Exception;
use Psr\Http\Message\ServerRequestInterface;
#[PHA\Route('/entity')] //This is how you set path for handler operations
class Handler
{
#[PHA\Post()] //This is how you declare HTTP method of the operation
#[PHA\Consumer('application/json', 'Json')] //This is how you consume request body
#[PHA\Attribute('Transfer', ['type'=>'My\DTO','object_attr'=>'dto','error_attr'=>'errors'])] //This is how you attribute request
#[PHA\Producer('application/json', 'Json')] //This is how you produce response body from returned value
public function handlePost(ServerRequestInterface $request): \My\DTO
{
$errors = $request->getAttribute('errors');
if (!empty($errors))
{
//This is how you can return non-200 responses
throw new Exception\UnprocessableEntity($errors);
}
/* @var \My\DTO $dto */
$dto = $request->getAttribute('dto');
return $dto;
}
}