PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 29, 2021

[FIXED] Connecting PHP to React

 December 29, 2021     connect, lamp, php, reactjs     No comments   

Issue

I've been learning how to use React and I already know substantial PHP. I know that I can use PHP with React since React is only the view in MVC but how do I use PHP with react locally. I have a lamp server running on my Linux machine I want to be able to run it as a backend. Like, let's say I want to make a login screen how do I connect it up. I make the front end in React and the backend in PHP. Do I have to use CORS I'd rather not since, in the end, it's going to be running on the same server.


Solution

You don't need CORS, it seems you are looking for proxy.

Just set your proxy to the listening port of your LAMP API server and then simply use fetch with absolute path from your React app.

Let's say your LAPM server is listening on port 23000

"proxy": "http://localhost:23000",

in package.json file of your React app, then

fetch("/my/api")

will call http://localhost:23000/my/api.



Answered By - Daniele Ricci
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing