React.js and PHP together?

48,890

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.

Share:
48,890
cakesofwrath
Author by

cakesofwrath

Updated on July 09, 2022

Comments

  • cakesofwrath
    cakesofwrath almost 2 years

    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.)