Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js and PHP together?

Tags:

I'm looking to rewrite an old website using React.js for my frontend framework and PHP for the little backend stuff I may need.

The server I'm using is IIS with Apache, but I have little/no SSH access/ability to install stuff on the machine (i.e. npm, node, etc.), so I'm restricted to using PHP as a backend (so I can't build/compile my React JSX serverside). However, couldn't I simply just upload built/compiled my react files to the server?

I have seen this, but I'm not entirely sure that I need it?

Basically, my question is, is using these two together practical? Would I need any other tools to accomplish this? (The website is likely to be pretty small.)

like image 468
cakesofwrath Avatar asked Jul 10 '15 20:07

cakesofwrath


People also ask

Can you use React and PHP together?

Now can we use React js with Php ? Yes. This is possible. Reactjs is just the 'V' in MVC.

Is React and PHP good?

The big difference between React and PHP is that React is better suited for building user interfaces while PHP is better suited for backend development tasks. However, they also have some key differences that can affect how you develop your website or web application.

Can we use PHP as a backend With React?

Yes of course, you can build a backend with PHP using a framework like CodeIgniter or Laravel or vanilla PHP and build your API and serve your React App with the data, or you can use Node. js for the backend or Java spring boot or .

Can we use MySQL with React JS?

Yes. We are building an end-to-end project with React + Express + Node. js + MySQL. MySQL is an open-source relational database management system (RDBMS).


1 Answers

I've used PHP and React together briefly (and then moved on to isomorphic React). Running V8 with PHP is only useful if you need server-side rendering (for fast initial loading and easy SEO), but I've not tried it so I'm not sure whether it's stable/reliable enough. If you do not care about server-side rendering, then you can just build the React app and include it in your PHP view.

Basically, the PHP view would serve as a layout, with a React container element defined so that your React app can bootstrap with it.

You can also pre-fetch the initial data for the React app with PHP and somehow attach it to your PHP view. The simplest way would be to use a script block to assign the JSON-serialized data to a global variable. Another way would be to define element(s) and attach your JSON-serialized data as element attributes, to avoid globals. Either way, you'd have your Flux stores bootstrap with those initial data to avoid having to hit APIs before the app can load.

like image 65
Hans Avatar answered Sep 21 '22 20:09

Hans