K
K
kafkiansky2019-01-05 13:25:21
symfony
kafkiansky, 2019-01-05 13:25:21

How to make friends React with Symfony?

I have already seen similar questions here on the toaster, but for me they were not exhaustive, unfortunately. How to connect React with Symfony after all? React should have its own folder, right? Let's say frontend. Communication takes place via json, this is also understandable. And then how? Symphony sends data to one route and react catches it there? Let's take an example:

/**
  *@Route("/posts")
  *
  /
public function index()
{
     return new JsonResponse($this->postRepository->findAllAsArray());
{

Does react already catch this data on the same route? Or how?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Anton Spirin, 2019-01-05
@neliudov

1. Submit for all page routes either a Twig template with div id=root and your bundle. Or, for this task, you raise a Node server (express / koa). Use Node server if you need SSR.
2. We write the initial static data, for example, in window.__INITIAL_STATE__, when initialized on the client, we send them to the store.
3. All communication between the client and server parts through the REST API.
4. On the client side, use the client router (react-router-dom). If using SSR on the server side use StaticRouter.
Perhaps everything.

A
Artem Spiridonov, 2019-01-05
@customtema

phpfaq.ru/newbie/na_tanke
https://ru.wikipedia.org/wiki/AJAX

G
grinat, 2019-01-06
@grinat

Depending on what you want, if spa, then all the static is given from the folder with the react, from the symphony only the rest api, the rest routes are different. If nginx, then for example, all routes like /api/bla-bal are redirected to symphony, the rest to the folder with the front.
If separate pages are on the react, then the symphony simply gives separate views, in these views there is already a react.
If ssr, then the same as in the first paragraph, only all non-IP routes go to the node, which renders html on the server and sends it to the browser along with js, it makes sense to bother with ssr if you need indexing in Yandex, in any case it's a headache front.

A
Andrey Titov, 2020-02-25
@titov_andrei

How to use React in Symfony 4?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question