S
S
Steve2022-01-06 18:19:02
PHP
Steve, 2022-01-06 18:19:02

How to combine front and back?

The project has a separate front and back. How to combine them correctly? Is this implementation normal?

<?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);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Savelyev, 2022-01-07
@AgentSmith

The front and back are combined on the site itself. The front is the client, all request processing is on the back. Everything is interconnected

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question