简体   繁体   English

连接反应到 php 的最佳方式

[英]Best way to connect react to php

I am wondering what is the best way to connect backend wrote in php to front end code in ReactJS.我想知道将用 php 编写的后端连接到 ReactJS 中的前端代码的最佳方法是什么。

What is an option for that idea?这个想法有什么选择?

I am sure that when application have on backend laravel then I can use laravel-mix to use react on frontend, but is there any alternatives?我确信当应用程序在后端 laravel 上时,我可以使用 laravel-mix 在前端使用 react,但是有没有其他选择?

Maybe implement react to symfony, CakePHP or maybe just only simple PHP can run react on frontend.也许实现对 symfony、CakePHP 的反应,或者可能只是简单的PHP可以在前端运行反应。

I'm working on react application right now it is running on nodeJS but It is important for me to use PHP and MySQL on backend.我的工作,现在它正在运行的NodeJS应用程序的反应,但对我对后端使用PHPMySQL是很重要的。

So I am looking for the easiest and cleanest solution.所以我正在寻找最简单、最干净的解决方案。 Thank You for help.谢谢你的帮助。

it is easy this is very clean explaining with example and steps 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', {
  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);
      });
}

You should separate your frontend and backend stacks, with a REST interface in between.您应该将前端和后端堆栈分开,中间有一个 REST 接口。 With the architecture, you will have more control of your both frontend and backend to choose your favorite tools, languages etc. For instance, you can work on React.js + Webpack + Babel for your frontend and Laravel / Symfony for the backend.使用该架构,您将可以更好地控制前端和后端,以选择您喜欢的工具、语言等。例如,您可以在前端使用 React.js + Webpack + Babel,后端使用 Laravel / Symfony。

Please see this article with more details and advantages of doing this way.请参阅这篇文章,了解更多详细信息和这样做的优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM