连接的最佳方式反应到PHP

时间:2017-07-03 19:51:51

标签: php node.js reactjs

我想知道将在php中写入的后端连接到ReactJS中的前端代码的最佳方法是什么。

这个想法有什么选择?

我确信当应用程序有后端laravel然后我可以使用laravel-mix来使用前端的反应,但有没有其他选择?

也许实现对symfony的反应,CakePHP或者只是简单的 PHP 可以在前端运行反应。

我现在正在 nodeJS 上运行反应应用程序但是对我来说使用 PHP 非常重要后端 MySQL

所以我正在寻找最简单,最干净的解决方案。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

很容易 这是非常干净的示例和步骤enter link description here

的解释

 
fetch('https://reactnativecode.000webhostapp.com/user_registration.php', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
 
    name: UserName,
 
    email: UserEmail,
 
    password: UserPassword
 
  })
 
}).then((response) => response.json())
      .then((responseJson) => {
 
// Showing response message coming from the server after inserting records.
        Alert.alert(responseJson);
 
      }).catch((error) => {
        console.error(error);
      });
 
 
  }

fetch('https://reactnativecode.000webhostapp.com/user_registration.php',{   方法:“ POST”,   标头:{     'Accept':'application / json',     'Content-Type':'application / json',   },   正文:JSON.stringify({

name: UserName,

email: UserEmail,

password: UserPassword

})

})。then((response)=> response.json())       .then((responseJson)=> {

//显示插入记录后来自服务器的响应消息。         Alert.alert(responseJson);

  }).catch((error) => {
    console.error(error);
  });

}

答案 1 :(得分:0)

您应该将前端和后端堆栈分开,中间使用REST接口。使用该体系结构,您可以更好地控制前端和后端,以选择您喜欢的工具,语言等。例如,您可以使用React.js + Webpack + Babel作为前端,使用Laravel / Symfony作为后端。

请参阅此article,了解更多详情和优势。