DEV Community

Discussion on: Abstracting with react hooks on LSD

Collapse
 
patheticgeek profile image
Pathetic Geek

I haven't worked that much on larvel so can't say much. But what I will say is if you're using react and php you definately want to keep your client and server seperate because in my experience php doesn't play very well if we have the client side in the same code specially some framework like react, vue etc.
So you can use react with php APIs but i wont recommend having them mixed in same project like what you might see in a traditional php project.

Collapse
 
askoflor profile image
askoflor

you say not to mix react and php as in a traditional project but my question is the following how to separate them and make them work in production together

Thread Thread
 
patheticgeek profile image
Pathetic Geek • Edited

So you will have 2 diffrent projects one which will be your php server and it will only serve JSON data and one will be your react app which will call the php APIs get JSON and display it.

In local you can start your php project at localhost:8081 and your react project at localhost:8080 and when you want to call any api you can call it at localhost:8081/test.

And in prod your php project will be deployed at lets say api.example.com
and react project will be deployed at example.com. So when you want to call any apis in prod you send a request to api.example.com/test.

Axios (axios-http.com/) is a good library for making this kind of requests, you can do somthing like below

// check if we're in dev and set base url correctly
const baseURL = process.env.NODE_ENV === 'development' ? 'localhost:8081' : 'api.example.com';
// create a axios instance
const axios = Axios.create({ baseURL })
// send a request
axios.get('/test').then(res => console.log(res))
Enter fullscreen mode Exit fullscreen mode

What this will do is set the correct base url and whenever you send a request it will add the base url before the path ('/test') so your requests will go to right address.

With this your server (php) and client (react) are two different things and you can easily swap react for vue or angular or swap php for ruby on rails or python.

Thread Thread
 
askoflor profile image
askoflor

okay with this method if we realize a web application the loading speed of the site and the seo referencing will be obtained

Thread Thread
 
agnic profile image
agni-c • Edited

If you are not intending to use client server architecture , It may be wise to use vue instead of react. Laravel even sponsoring vue because it is solving a lot of it's problems without much tooling like react.