В проекте раздельный фронт и бек. Как их правильно объединить? Нормальна ли такая реализация?
<?php
namespace App\Controller\API;
use App\Form\LoginType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class LoginController extends AbstractController
{
private $client;
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}
... // API
/**
* @param Request $request
*
* @return JsonResponse
*/
public function loginAction(Request $request): JsonResponse
{
$form = $this->createForm(LoginType::class);
$form->handleRequest($request);
$username = $this->getUser()->getUsername();
return new JsonResponse(['name' => $username]);
}
...
/**
* @param Request $request
* @return array
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function loginAction(Request $request): JsonResponse
{
$response = $this->client->request(
'POST',
'http://site.loc/api/login/', [
'body' => [
'login' => $request->request->get('login'),
'password' => $request->request->get('pass')
]
]
);
$content = $response->toArray();
return new JsonResponse($content);
}
}