React.js 和 PHP 在一起吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31349765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:23:14  来源:igfitidea点击:

React.js and PHP together?

phpreactjs

提问by cakesofwrath

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.

我希望使用 React.js 为我的前端框架和 PHP 为我可能需要的小后端内容重写一个旧网站。

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?

我使用的服务器是带有 Apache 的 IIS,但我几乎没有/没有 SSH 访问/在机器上安装东西的能力(即 npm、节点等),所以我仅限于使用 PHP 作为后端(所以我无法构建/编译我的 React JSX 服务器端)。但是,我不能简单地将构建/编译的 React 文件上传到服务器吗?

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

基本上,我的问题是,将这两者结合使用是否实用?我是否需要任何其他工具来完成此操作?(该网站可能很小。)

回答by Hans

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.

我曾短暂地一起使用过 PHP 和 React(然后转向同构 React)。仅当您需要服务器端渲染(用于快速初始加载和轻松 SEO)时,使用 PHP 运行 V8 才有用,但我还没有尝试过,所以我不确定它是否足够稳定/可靠。如果您不关心服务器端渲染,那么您可以构建 React 应用程序并将其包含在您的 PHP 视图中。

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

基本上,PHP 视图将用作布局,定义了一个 React 容器元素,以便您的 React 应用程序可以使用它进行引导。

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.

您还可以使用 PHP 预取 React 应用程序的初始数据,并以某种方式将其附加到您的 PHP 视图。最简单的方法是使用脚本块将 JSON 序列化数据分配给全局变量。另一种方法是定义元素并将您的 JSON 序列化数据附加为元素属性,以避免全局变量。无论哪种方式,您都可以使用这些初始数据引导您的 Flux 存储,以避免在应用程序加载之前必须点击 API。