{"id":351964,"date":"2024-05-20T21:59:21","date_gmt":"2024-05-20T21:59:21","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=351964"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-20T21:59:21","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=351964","title":{"rendered":"<span>Symfony Json RPC API Bundle \u2014 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 API \u0441\u043e \u0432\u0441\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u043c<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u043b\u0435\u0433\u043a\u043e \u0438 \u0431\u044b\u0441\u0442\u0440\u043e \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c API \u043d\u0430 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 Symfony \u0441 \u0443\u0436\u0435 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443?<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0430\u043d\u0434\u043b\u043e\u043c \u0434\u043b\u044f Symfony 6+<\/p>\n<p><a href=\"https:\/\/packagist.org\/packages\/otezvikentiy\/json-rpc-api\" rel=\"noopener noreferrer nofollow\">\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 packagist<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/OtezVikentiy\/symfony-jsonrpc-api-bundle\" rel=\"noopener noreferrer nofollow\">\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 github<\/a><\/p>\n<h4>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0431\u0430\u043d\u0434\u043b\u0430<\/h4>\n<p>\u041a\u0430\u043a \u0438 \u043b\u044e\u0431\u0430\u044f \u0434\u0440\u0443\u0433\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0441\u0442\u0430\u0432\u0438\u0442\u0441\u044f \u044d\u0442\u043e\u0442 \u0431\u0430\u043d\u0434\u043b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u043f\u043e\u0437\u0435\u0440\u0430:<\/p>\n<pre><code>composer require otezvikentiy\/json-rpc-api<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0431\u0430\u043d\u0434\u043b \u043f\u043e\u043f\u0430\u043b \u0432 \u0438\u043d\u0441\u0442\u0430\u043b\u043b\u044f\u0446\u0438\u044e \u0432 \u0444\u0430\u0439\u043b\u0435 config\/bundles.php \u0444\u0430\u0439\u043b\u0435:<\/p>\n<pre><code>&lt;?php \/\/ config\/bundles.php return [     \/\/...     OV\\JsonRPCAPIBundle\\OVJsonRPCAPIBundle::class => ['all' => true], ];<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u0430\u0439\u043b\u00a0config\/routes\/ov_json_rpc_api.yaml \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c:<\/p>\n<pre><code># config\/routes\/ov_json_rpc_api.yaml ov_json_rpc_api:    resource: '@OVJsonRPCAPIBundle\/config\/routes\/routes.yaml'<\/code><\/pre>\n<p>\u0418 \u0432 \u0444\u0430\u0439\u043b\u0438\u043a\u0435 config\/services.yaml \u0432 \u0441\u0435\u043a\u0446\u0438\u044e services \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b \u0441 \u0442\u043e\u0439 \u043f\u0430\u043f\u043a\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0445 \u0432\u0430\u0448\u0438\u0445 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 API. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a:<\/p>\n<pre><code># config\/services.yaml services:     App\\RPC\\V1\\:         resource: '..\/src\/RPC\/V1\/{*Method.php}'         tags:             - { name: ov.rpc.method, namespace: App\\RPC\\V1\\, version: 1 }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451! \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u0430 &#8212; \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0441\u0432\u043e\u0435\u0433\u043e API!\u00a0<\/p>\n<h4>\u0418\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0432\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b<\/h4>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0430\u0448 \u043f\u0435\u0440\u0432\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 API. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043c\u0435\u0442\u043e\u0434. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c 3 \u0444\u0430\u0439\u043b\u0438\u043a\u0430 \u0432\u043e\u0442 \u043f\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u043e\u0442 \u0441\u0445\u0435\u043c\u0435:<\/p>\n<pre><code>\u2514\u2500\u2500 src     \u2514\u2500\u2500 RPC         \u2514\u2500\u2500 V1             \u2514\u2500\u2500 getProducts                 \u251c\u2500\u2500 GetProductsRequest.php                 \u2514\u2500\u2500 GetProductsResponse.php             \u2514\u2500\u2500 GetProductsMethod.php<\/code><\/pre>\n<pre><code>&lt;?php  namespace App\\RPC\\V1\\getProducts;  class GetProductsRequest {     private int $id;     private string $title;      \/**      * @param int $id      *\/     public function __construct(int $id)     {         $this->id = $id;     }          \/**      * @return int      *\/     public function getId(): int     {         return $this->id;     }          \/**      * @param int $id      *\/     public function setId(int $id): void     {         $this->id = $id;     }          \/**      * @return string      *\/     public function getTitle(): string     {         return $this->title;     }          \/**      * @param string $title      *\/     public function setTitle(string $title): void     {         $this->title = $title;     }  }<\/code><\/pre>\n<pre><code>&lt;?php  namespace App\\RPC\\V1\\getProducts;  class GetProductsResponse {     private bool $success;     private string $title;      \/**      * @param string $title      * @param bool $success      *\/     public function __construct(string $title, bool $success = true)     {         $this->success = $success;         $this->title = $title;     }          \/**      * @return string      *\/     public function getTitle(): string     {         return $this->title;     }          \/**      * @param string $title      *\/     public function setTitle(string $title): void     {         $this->title = $title;     }          \/**      * @return bool      *\/     public function isSuccess(): bool     {         return $this->success;     }          \/**      * @param bool $success      *\/     public function setSuccess(bool $success): void     {         $this->success = $success;     }  }<\/code><\/pre>\n<pre><code>&lt;?php  namespace App\\RPC\\V1;  use OV\\JsonRPCAPIBundle\\Core\\Annotation\\JsonRPCAPI; use App\\RPC\\V1\\getProducts\\GetProductsRequest; use App\\RPC\\V1\\getProducts\\GetProductsResponse;  \/**  *  * @JsonRPCAPI(methodName = \"getProducts\")  *\/ #[JsonRPCAPI(methodName: 'getProducts')] class GetProductsMethod {     \/**      * @param GetProductsRequest $request      * @return GetProductsResponse      *\/     public function call(GetProductsRequest $request): GetProductsResponse     {         \/\/ \u0437\u0434\u0435\u0441\u044c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0435 \u0432\u0441\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u0430\u0448\u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 API         $id = $request->getId();         return new GetProductsResponse($request->getTitle().'OLOLOLOLO');     } }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c curl-\u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a:<\/p>\n<pre><code>curl --header \"Content-Type: application\/json\" --request POST --data '{\"jsonrpc\": \"2.0\",\"method\": \"getProducts\",\"params\": {\"title\": \"AZAZAZA\"},\"id\": 1}' http:\/\/localhost\/api\/v1<\/code><\/pre>\n<p>\u0418 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043e\u0442\u0432\u0435\u0442:<\/p>\n<pre><code>{\"jsonrpc\":\"2.0\",\"result\":{\"title\":\"AZAZAZAOLOLOLOLO\",\"success\":true},\"id\":null}<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e Json RPC API \u0432\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0441\u0435\u0433\u043e 3 \u043a\u043b\u0430\u0441\u0441\u0430.<\/p>\n<h4>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443<\/h4>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u00a0<a href=\"https:\/\/symfony.com\/doc\/current\/security\/custom_authenticator.html\" rel=\"noopener noreferrer nofollow\">\u043f\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 symfony<\/a>, \u043d\u043e \u0432\u0441\u0435 \u0436\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u0435\u0449\u0435 \u0440\u0430\u0437\u043e\u043a.\u00a0<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0434\u043e\u0441\u0442\u0443\u043f \u0432 \u0432\u0430\u0448\u0435 API \u0431\u044b\u043b \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443 &#8212; \u0432\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0447\u0442\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c:<\/p>\n<p>1) \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u0448\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0411\u0414 \u0432 \u0444\u0430\u0439\u043b\u0435 src\/Entity\/ApiToken.php<\/p>\n<pre><code>&lt;?php  namespace App\\Entity;  use DateTime; use DateTimeInterface; use Doctrine\\DBAL\\Types\\Types; use Doctrine\\ORM\\Mapping as ORM;  #[ORM\\Entity] class ApiToken {     #[ORM\\Id]     #[ORM\\GeneratedValue]     #[ORM\\Column]     private int $id;      #[ORM\\Column(type: 'string', length: 500, nullable: false)]     private string $token;      #[ORM\\Column(type: Types::DATETIME_MUTABLE, nullable: false)]     private DateTimeInterface $expiresAt;      #[ORM\\ManyToOne(inversedBy: 'apiTokens')]     #[ORM\\JoinColumn(nullable: false)]     private User $user;      \/**      * @return int      *\/     public function getId(): int     {         return $this->id;     }          \/**      * @param int $id      *      * @return ApiToken      *\/     public function setId(int $id): ApiToken     {         $this->id = $id;              return $this;     }          \/**      * @return string      *\/     public function getToken(): string     {         return $this->token;     }          \/**      * @param string $token      *      * @return ApiToken      *\/     public function setToken(string $token): ApiToken     {         $this->token = $token;              return $this;     }          \/**      * @return DateTimeInterface      *\/     public function getExpiresAt(): DateTimeInterface     {         return $this->expiresAt;     }          \/**      * @param DateTimeInterface $expiresAt      *      * @return ApiToken      *\/     public function setExpiresAt(DateTimeInterface $expiresAt): ApiToken     {         $this->expiresAt = $expiresAt;              return $this;     }          \/**      * @return User      *\/     public function getUser(): User     {         return $this->user;     }          \/**      * @param User $user      *      * @return ApiToken      *\/     public function setUser(User $user): ApiToken     {         $this->user = $user;              return $this;     }          \/**      * @return bool      *\/     public function isValid(): bool     {         return (new DateTime())->getTimestamp() > $this->expiresAt->getTimestamp();     }  }<\/code><\/pre>\n<p>2) \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0432 \u0444\u0430\u0439\u043b\u0435 src\/Security\/ApiKeyAuthenticator.php<\/p>\n<pre><code>&lt;?php  namespace App\\Security;  use App\\Entity\\ApiToken; use App\\Entity\\User; use Doctrine\\ORM\\EntityManagerInterface; use Symfony\\Component\\HttpFoundation\\JsonResponse; use Symfony\\Component\\HttpFoundation\\Request; use Symfony\\Component\\HttpFoundation\\Response; use Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface; use Symfony\\Component\\Security\\Core\\Exception\\AuthenticationException; use Symfony\\Component\\Security\\Core\\Exception\\CustomUserMessageAuthenticationException; use Symfony\\Component\\Security\\Http\\Authenticator\\AbstractAuthenticator; use Symfony\\Component\\Security\\Http\\Authenticator\\Passport\\Badge\\UserBadge; use Symfony\\Component\\Security\\Http\\Authenticator\\Passport\\Passport; use Symfony\\Component\\Security\\Http\\Authenticator\\Passport\\SelfValidatingPassport;  class ApiKeyAuthenticator extends AbstractAuthenticator {     \/**      * @param EntityManagerInterface $em      *\/     public function __construct(         private readonly EntityManagerInterface $em     ){     }      \/**      * @param Request $request      *      * @return bool      *\/     public function supports(Request $request): bool     {         return str_contains($request->getRequestUri(), '\/api\/v');     }          \/**      * @param Request $request      *      * @return Passport      *\/     public function authenticate(Request $request): Passport     {         $apiToken = $request->headers->get('X-AUTH-TOKEN');         if (null === $apiToken) {             throw new CustomUserMessageAuthenticationException('No API token provided');         }              $apiTokenEntity = $this->em->getRepository(ApiToken::class)->findOneBy(['token' => $apiToken]);         if (is_null($apiTokenEntity)) {             throw new CustomUserMessageAuthenticationException('No API token provided');         }              return new SelfValidatingPassport(new UserBadge(             $apiTokenEntity->getUser()->getId(),             function () use ($apiTokenEntity) {                 return $this->em->getRepository(User::class)->find($apiTokenEntity->getUser()->getId());             }         ));     }          \/**      * @param Request        $request      * @param TokenInterface $token      * @param string         $firewallName      *      * @return Response|null      *\/     public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response     {         return null;     }          \/**      * @param Request                 $request      * @param AuthenticationException $exception      *      * @return Response|null      *\/     public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response     {         $data = [             'message' => strtr($exception->getMessageKey(), $exception->getMessageData())         ];              return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);     }  }<\/code><\/pre>\n<p>3) \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 firewall \u0432 \u0444\u0430\u0439\u043b\u0435 config\/security.yaml<\/p>\n<pre><code>security:     # https:\/\/symfony.com\/doc\/current\/security.html#registering-the-user-hashing-passwords     password_hashers:         Symfony\\Component\\Security\\Core\\User\\PasswordAuthenticatedUserInterface: 'auto'     # https:\/\/symfony.com\/doc\/current\/security.html#loading-the-user-the-user-provider     providers:         # used to reload user from session &amp; other features (e.g. switch_user)         app_user_provider:             entity:                 class: App\\Entity\\User                 property: email     firewalls:         api:             pattern: ^\/api             provider: app_user_provider             custom_authenticators:                 - App\\Security\\ApiKeyAuthenticator<\/code><\/pre>\n<p>4) \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u0442\u043e\u043a\u0435\u043d\u0430, \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044e, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u0438 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f. ))) \u0421 \u0442\u043e\u043a\u0435\u043d\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441 curl \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code>curl --header \"X-AUTH-TOKEN: your-token-here\" --header \"Content-Type: application\/json\" --request POST --data '{\"jsonrpc\": \"2.0\",\"method\": \"getProducts\",\"params\": {\"title\": \"AZAZAZA\"},\"id\": 1}' http:\/\/localhost\/api\/v1<\/code><\/pre>\n<p><a href=\"https:\/\/otezvikentiy.tech\/articles\/symfony-json-rpc-api-bundle-prostoe-api-so-vsem-neobhodimym\" rel=\"noopener noreferrer nofollow\">\u041e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/754134\/\"> https:\/\/habr.com\/ru\/articles\/754134\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u043b\u0435\u0433\u043a\u043e \u0438 \u0431\u044b\u0441\u0442\u0440\u043e \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c API \u043d\u0430 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 Symfony \u0441 \u0443\u0436\u0435 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443?<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0430\u043d\u0434\u043b\u043e\u043c \u0434\u043b\u044f Symfony 6+<\/p>\n<p><a href=\"https:\/\/packagist.org\/packages\/otezvikentiy\/json-rpc-api\" rel=\"noopener noreferrer nofollow\">\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 packagist<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/OtezVikentiy\/symfony-jsonrpc-api-bundle\" rel=\"noopener noreferrer nofollow\">\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 github<\/a><\/p>\n<h4>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0431\u0430\u043d\u0434\u043b\u0430<\/h4>\n<p>\u041a\u0430\u043a \u0438 \u043b\u044e\u0431\u0430\u044f \u0434\u0440\u0443\u0433\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0441\u0442\u0430\u0432\u0438\u0442\u0441\u044f \u044d\u0442\u043e\u0442 \u0431\u0430\u043d\u0434\u043b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u043f\u043e\u0437\u0435\u0440\u0430:<\/p>\n<pre><code>composer require otezvikentiy\/json-rpc-api<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0431\u0430\u043d\u0434\u043b \u043f\u043e\u043f\u0430\u043b \u0432 \u0438\u043d\u0441\u0442\u0430\u043b\u043b\u044f\u0446\u0438\u044e \u0432 \u0444\u0430\u0439\u043b\u0435 config\/bundles.php \u0444\u0430\u0439\u043b\u0435:<\/p>\n<pre><code>&lt;?php \/\/ config\/bundles.php return [     \/\/...     OV\\JsonRPCAPIBundle\\OVJsonRPCAPIBundle::class => ['all' => true], ];<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u0430\u0439\u043b\u00a0config\/routes\/ov_json_rpc_api.yaml \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c:<\/p>\n<pre><code># config\/routes\/ov_json_rpc_api.yaml ov_json_rpc_api:    resource: '@OVJsonRPCAPIBundle\/config\/routes\/routes.yaml'<\/code><\/pre>\n<p>\u0418 \u0432 \u0444\u0430\u0439\u043b\u0438\u043a\u0435 config\/services.yaml \u0432 \u0441\u0435\u043a\u0446\u0438\u044e services \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b \u0441 \u0442\u043e\u0439 \u043f\u0430\u043f\u043a\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0445 \u0432\u0430\u0448\u0438\u0445 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 API. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a:<\/p>\n<pre><code># config\/services.yaml services:     App\\RPC\\V1\\:         resource: '..\/src\/RPC\/V1\/{*Method.php}'         tags:             - { name: ov.rpc.method, namespace: App\\RPC\\V1\\, version: 1 }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451! \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u0430 &#8212; \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0441\u0432\u043e\u0435\u0433\u043e API!\u00a0<\/p>\n<h4>\u0418\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0432\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b<\/h4>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0430\u0448 \u043f\u0435\u0440\u0432\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 API. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043c\u0435\u0442\u043e\u0434. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c 3 \u0444\u0430\u0439\u043b\u0438\u043a\u0430 \u0432\u043e\u0442 \u043f\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u043e\u0442 \u0441\u0445\u0435\u043c\u0435:<\/p>\n<pre><code>\u2514\u2500\u2500 src     \u2514\u2500\u2500 RPC         \u2514\u2500\u2500 V1             \u2514\u2500\u2500 getProducts                 \u251c\u2500\u2500 GetProductsRequest.php                 \u2514\u2500\u2500 GetProductsResponse.php             \u2514\u2500\u2500 GetProductsMethod.php<\/code><\/pre>\n<pre><code>&lt;?php  namespace App\\RPC\\V1\\getProducts;  class GetProductsRequest {     private int $id;     private string $title;      \/**      * @param int $id      *\/     public function __construct(int $id)     {         $this->id = $id;     }          \/**      * @return int      *\/     public function getId(): int     {         return $this->id;     }          \/**      * @param int $id      *\/     public function setId(int $id): void     {         $this->id = $id;     }          \/**      * @return string      *\/     public function getTitle(): string     {         return $this->title;     }          \/**      * @param string $title      *\/     public function setTitle(string $title): void     {         $this->title = $title;     }  }<\/code><\/pre>\n<pre><code>&lt;?php  namespace App\\RPC\\V1\\getProducts;  class GetProductsResponse {     private bool $success;     private string $title;      \/**      * @param string $title      * @param bool $success      *\/     public function __construct(string $title, bool $success = true)     {         $this->success = $success;         $this->title = $title;     }          \/**      * @return string      *\/     public function getTitle(): string     {         return $this->title;     }          \/**      * @param string $title      *\/     public function setTitle(string $title): void     {         $this->title = $title;     }          \/**      * @return bool      *\/     public function isSuccess(): bool     {         return $this->success;     }          \/**      * @param bool $success      *\/     public function setSuccess(bool $success): void     {         $this->success = $success;     }  }<\/code><\/pre>\n<pre><code>&lt;?php  namespace App\\RPC\\V1;  use OV\\JsonRPCAPIBundle\\Core\\Annotation\\JsonRPCAPI; use App\\RPC\\V1\\getProducts\\GetProductsRequest; use App\\RPC\\V1\\getProducts\\GetProductsResponse;  \/**  *  * @JsonRPCAPI(methodName = \"getProducts\")  *\/ #[JsonRPCAPI(methodName: 'getProducts')] class GetProductsMethod {     \/**      * @param GetProductsRequest $request      * @return GetProductsResponse      *\/     public function call(GetProductsRequest $request): GetProductsResponse     {         \/\/ \u0437\u0434\u0435\u0441\u044c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0435 \u0432\u0441\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u0430\u0448\u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 API         $id = $request->getId();         return new GetProductsResponse($request->getTitle().'OLOLOLOLO');     } }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c curl-\u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a:<\/p>\n<pre><code>curl --header \"Content-Type: application\/json\" --request POST --data '{\"jsonrpc\": \"2.0\",\"method\": \"getProducts\",\"params\": {\"title\": \"AZAZAZA\"},\"id\": 1}' http:\/\/localhost\/api\/v1<\/code><\/pre>\n<p>\u0418 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043e\u0442\u0432\u0435\u0442:<\/p>\n<pre><code>{\"jsonrpc\":\"2.0\",\"result\":{\"title\":\"AZAZAZAOLOLOLOLO\",\"success\":true},\"id\":null}<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e Json RPC API \u0432\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0441\u0435\u0433\u043e 3 \u043a\u043b\u0430\u0441\u0441\u0430.<\/p>\n<h4>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443<\/h4>\n<p>\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u00a0<a href=\"https:\/\/symfony.com\/doc\/current\/security\/custom_authenticator.html\" rel=\"noopener noreferrer nofollow\">\u043f\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 symfony<\/a>, \u043d\u043e \u0432\u0441\u0435 \u0436\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u0435\u0449\u0435 \u0440\u0430\u0437\u043e\u043a.\u00a0<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0434\u043e\u0441\u0442\u0443\u043f \u0432 \u0432\u0430\u0448\u0435 API \u0431\u044b\u043b \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443 &#8212; \u0432\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0447\u0442\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c:<\/p>\n<p>1) \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u0448\u0435\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0411\u0414 \u0432 \u0444\u0430\u0439\u043b\u0435 src\/Entity\/ApiToken.php<\/p>\n<pre><code>&lt;?php  namespace App\\Entity;  use DateTime; use DateTimeInterface; use Doctrine\\DBAL\\Types\\Types; use Doctrine\\ORM\\Mapping as ORM;  #[ORM\\Entity] class ApiToken {     #[ORM\\Id]     #[ORM\\GeneratedValue]     #[ORM\\Column]     private int $id;      #[ORM\\Column(type: 'string', length: 500, nullable: false)]     private string $token;      #[ORM\\Column(type: Types::DATETIME_MUTABLE, nullable: false)]     private DateTimeInterface $expiresAt;      #[ORM\\ManyToOne(inversedBy: 'apiTokens')]     #[ORM\\JoinColumn(nullable: false)]     private User $user;      \/**      * @return int      *\/     public function getId(): int     {         return $this->id;     }          \/**      * @param int $id      *      * @return ApiToken      *\/     public function setId(int $id): ApiToken     {         $this->id = $id;              return $this;     }          \/**      * @return string      *\/     public function getToken(): string     {         return $this->token;     }          \/**      * @param string $token      *      * @return ApiToken      *\/     public function setToken(string $token): ApiToken     {         $this->token = $token;              return $this;     }          \/**      * @return DateTimeInterface      *\/     public function getExpiresAt(): DateTimeInterface     {         return $this->expiresAt;     }          \/**      * @param DateTimeInterface $expiresAt      *      * @return ApiToken      *\/     public function setExpiresAt(DateTimeInterface $expiresAt): ApiToken     {         $this->expiresAt = $expiresAt;              return $this;     }          \/**      * @return User      *\/     public function getUser(): User     {         return $this->user;     }          \/**      * @param User $user      *      * @return ApiToken      *\/     public function setUser(User $user): ApiToken     {         $this->user = $user;              return $this;     }          \/**      * @return bool      *\/     public function isValid(): bool     {         return (new DateTime())->getTimestamp() > $this->expiresAt->getTimestamp();     }  }<\/code><\/pre>\n<p>2) \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0432 \u0444\u0430\u0439\u043b\u0435 src\/Security\/ApiKeyAuthenticator.php<\/p>\n<pre><code>&lt;?php  namespace App\\Security;  use App\\Entity\\ApiToken; use App\\Entity\\User; use Doctrine\\ORM\\EntityManagerInterface; use Symfony\\Component\\HttpFoundation\\JsonResponse; use Symfony\\Component\\HttpFoundation\\Request; use Symfony\\Component\\HttpFoundation\\Response; use Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface; use Symfony\\Component\\Security\\Core\\Exception\\AuthenticationException; use Symfony\\Component\\Security\\Core\\Exception\\CustomUserMessageAuthenticationException; use Symfony\\Component\\Security\\Http\\Authenticator\\AbstractAuthenticator; use Symfony\\Component\\Security\\Http\\Authenticator\\Passport\\Badge\\UserBadge; use Symfony\\Component\\Security\\Http\\Authenticator\\Passport\\Passport; use Symfony\\Component\\Security\\Http\\Authenticator\\Passport\\SelfValidatingPassport;  class ApiKeyAuthenticator extends AbstractAuthenticator {     \/**      * @param EntityManagerInterface $em      *\/     public function __construct(         private readonly EntityManagerInterface $em     ){     }      \/**      * @param Request $request      *      * @return bool      *\/     public function supports(Request $request): bool     {         return str_contains($request->getRequestUri(), '\/api\/v');     }          \/**      * @param Request $request      *      * @return Passport      *\/     public function authenticate(Request $request): Passport     {         $apiToken = $request->headers->get('X-AUTH-TOKEN');         if (null === $apiToken) {             throw new CustomUserMessageAuthenticationException('No API token provided');         }              $apiTokenEntity = $this->em->getRepository(ApiToken::class)->findOneBy(['token' => $apiToken]);         if (is_null($apiTokenEntity)) {             throw new CustomUserMessageAuthenticationException('No API token provided');         }              return new SelfValidatingPassport(new UserBadge(             $apiTokenEntity->getUser()->getId(),             function () use ($apiTokenEntity) {                 return $this->em->getRepository(User::class)->find($apiTokenEntity->getUser()->getId());             }         ));     }          \/**      * @param Request        $request      * @param TokenInterface $token      * @param string         $firewallName      *      * @return Response|null      *\/     public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response     {         return null;     }          \/**      * @param Request                 $request      * @param AuthenticationException $exception      *      * @return Response|null      *\/     public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response     {         $data = [             'message' => strtr($exception->getMessageKey(), $exception->getMessageData())         ];              return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);     }  }<\/code><\/pre>\n<p>3) \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 firewall \u0432 \u0444\u0430\u0439\u043b\u0435 config\/security.yaml<\/p>\n<pre><code>security:     # https:\/\/symfony.com\/doc\/current\/security.html#registering-the-user-hashing-passwords     password_hashers:         Symfony\\Component\\Security\\Core\\User\\PasswordAuthenticatedUserInterface: 'auto'     # https:\/\/symfony.com\/doc\/current\/security.html#loading-the-user-the-user-provider     providers:         # used to reload user from session &amp; other features (e.g.<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-351964","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/351964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=351964"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/351964\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=351964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=351964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=351964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}