PHP code example of benycode / slim-request-deserialize

1. Go to this page and download the library: Download benycode/slim-request-deserialize 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/ */

    

benycode / slim-request-deserialize example snippets


declare(strict_types=1);

namespace App;

final class RequestData
{
    public string $username;

    public string $loginId;

    public string $publicKey;

    public function setUsername(string $username): self
    {
        $this->username = $username;
	return $this;
    }

    public function getUsername(): ?string 
    {
        return $this
	    ->username
	;
    }

    public function setLoginId(string $loginId): self
    {
        $this->loginId = $loginId;
	return $this;
    }

    public function getLoginId(): ?string
    {
	return $this
	    ->loginId
        ;
    }

    public function setPublicKey(string $publicKey): self
    {
	$this->publicKey = $publicKey;
	return $this;
    }

    public function getPublicKey(): ?string
    {
	return $this
	    ->publicKey
        ;
    }
}

use BenyCode\Slim\RequestDeserialize\RequestDeserializeMiddleware;
use Symfony\Component\Serializer\Encoder\JsonEncoder as DeserializeEncoder;
use App\RequestData;

$app = new \Slim\App();

$app->post('/api/any_end_point',function ($req, $res, $args) {
 
})
->add(new RequestDeserializeMiddleware([
    RequestData::class,
    DeserializeEncoder::FORMAT, // json encoder, but you can choice xml, yaml, csv
]))	
;

$app->run();

...
public function __invoke(
    ServerRequestInterface $request,
    ResponseInterface $response
): ResponseInterface {
    $requestData = $request
        ->getAttribute('request_data')
    ;
    
    $requestData->getUsername(); // get the username
    $requestData->getLoginId(); // get the login id
    ...
}
...