簡短的回答是:不是真的。
唯一明智的做法是將一個節點進程運行,哪個php可以請求渲染。這不是一個糟糕的解決方案,特別是對於高度緩存的頁面。
我建議在一個非常動態的方式將其設置:
<?php
function render_component_to_string($component, $data)
{
$url = "http://localhost:9000/components/"
. $component
. "?data="
. rawurlencode(json_encode($data));
return file_get_contents($url)
}
?>
<div id="myFilteredList">
<?= render_component_to_string("FilteredList",
array("items" => items, "title" => "My List")) ?>
</div>
在node.js中:
var app = require('express')(); // [email protected]
var React = require('react');
// create a dictionary of components
// Object.create(null) because the key is supplied
var components = Object.create(null);
components.FilteredList = require('./components/filtered-list.js');
app.get('/component/:componentName', function(req, res){
var component = components[req.params.componentName];
// safety first!
if (!component) {
console.error("Invalid component", req.params.componentName);
return res.send(404);
}
// more safety
try {
var data = JSON.parse(req.query.data);
}
catch (e) {
console.error("Invalid JSON", req.params.componentName, req.query.data);
return res.send(400);
}
// render and send the result back
try {
var result = React.renderComponentToString(component(data));
}
catch (e) {
console.error('Could not render', req.params.componentName,
'with props', data);
console.error(e.message, '\n', e.stack);
return res.send(400);
}
res.send(result);
});
app.listen(9000);
這當然假設你的組件是CommonJS的模塊。如果他們不是,這是這樣做的另一個原因!
我幾年沒用過php,所以如果我犯了什麼錯誤,請更新這個答案。
感謝您的詳細解答!這是一個很酷的解決方案,但我認爲這對我的小型需求來說是一種矯枉過正,無論是在複雜性和成本方面(對於Node服務器而言)。如果這是我擔心的事情的狀態,那麼我將不得不重寫我的組件,使其能夠將其注入到現有的DOM元素中,例如Marionette。無論如何,這真是太糟糕了,我真的很喜歡React :( – Ingro 2014-09-25 23:17:04
除了複雜性之外,你可以在與你的web服務器/ php應用程序相同的服務器/ vps上運行它,這就是它被寫入的工作方式(在本地主機上)。 – FakeRainBrigand 2014-09-25 23:28:22
不幸的是,我們沒有擁有最終網站上線的網絡服務器,所以我們不可能讓Node應用程序在其上運行:( – Ingro 2014-09-26 07:31:05