How to cross the PHP with React. This is a continuation of the publication-it is here.
Creation of the PHP and React
development environment at this stage we are going to start a real project using React.js and Php as Front-End.
There are things that you, as a developer, need to know or have to use php as a backend with React. Here is the main list!
have basic knowledge php
Know your react.js
Make sure that your computer is installed by node.js with npm.
Whether PHP (Apache or Nginx)
step-by-step process of using PHP as a backend using React as a front-end
step on using PHP as a bacand with React is installed in your system is quite simple and consists in the following. Fronthentia
Step 1: Open the terminal and execute the following command:
npx Create -react-React-App
Note: Before performing this command, you must go to the directory where the React folder should be located! To go to the desired catalog, add this command to the terminal.
CD doCumbs
After that you will have to wait until your React-application is there Created.
Step 2: Go to the folder of your project. To do this, open the project you created in the terminal.
CD My-React-App
Step 3: Launch the development server:
NPM Start
Creating the creation PHP-bekend
Now that your React application is ready to work, the time has come to create a PHP bacand:
mkdir pHP_Backend
Having completed this command, you will see a folder with the name php_backend.
Step 3: In the section «Index.PH, you can begin to determine the final points and logic PHP.» Below is a description in my php-backend/index.php file. If you are a PHP developer, you are familiar with this syntax.
& lt;? Php
$ servername = «Localhost»;
$ username = «Root»;
$ password = «»;
$ DATABASENAME = «React_PHP»;
$ Conn = MySQLI_CONNECT ($ SERVERNAME, $ Username, $ password, $ databaseename);
$ RECTEXT = $ _POST ['text'];
$ query = («Insert Into React_php (Texts) Values ('$ RECTEXT')») «);
if (MySqli_Query ($ Conn, $ Query)) {
Echo «Data Has Been Inserted Successfully»;
} else {
echo «error»;
}
? & gt;
Now you need to go to the previously created React Code and open the folder in the code editor. You will see a folder named SRC/App.js, and add the following code.
Import React, {Component} from React ';
//Import Logo from './logo.svg';
Import Axios from 'Axios'; //Import Axios
Import './App.css';
Class App Extends Component {
stat = {
text: «
}
handleadd = async e = & gt; {
await this.setstate ({
text: e.target.value
})
}
handlesubmit = e = & gt; {
e.preventdefault ();
concole.log (this.state.text);
let formData = New FormData ();
FormData.Append («Text», this.state.text);
const url = «http: //localhost: 80/React-backend/»
Axios.post (url, FormData)
.Then (res = & gt; concole.log (res.data))
.catch (err = & gt; console.log (err))
}
render () {
return (
& lt; div Classname = «App-header» & gt;
& lt; Input
o nchange = {this.Handleadd}
classname = «Form-Control»
//value = {this.state.text}
type = «text»
id = 'Text'
PLACEHOLDER = 'ENTER SOME TEXT!'/& GT;
& lt; Br/& gt;
& lt; Button
o nclick = {this.handlessubmit}
classname = «BTN BTN-SUCCESS»
ID = 'Submit' & GT; Save & LT;/Button & GT;
& LT;/Div & gt;
);
}
}
Export Defult App;
Note: PHP works thanks to Axios … Take a look at the code fragment below. You will see how we used it!
In the generally accessible folder of your project, you will see index.htmls I added a bootstrap CDN as a framework that I use.
& Lt; Link Rel = «Stylesheet» href = «https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css» & gt;
So, a little Stylization.
. App {
text-align: center;
}
. App -logo {
height: 40vmin;
pointer-events: none;
}
@media (Prefers-creed-motion: no-preference) {
.App -logo {
Animation: App-Logo-Spin Infinite 20s Linear;
}
}
. App-header {
background-color: #282c34;
min-heh;
display: flex> flex-direction: column;
align -iteMS: center;
justify-Content: Center;
font-Size: Calc (10px + 2vmin);
color: White;
}
. App-Link {
color: #61DAFB;
}
@keyframes app -logo-spin {
from {
transform: raotate (0deg);
}
to {
transform: raotate (360deg);
}
}
by pressing the ynter now-you will see a miracle.
Conclusion
In conclusion, we note that the integration of PHP as a front-line as a frontal and a universal solution for Development of modern web applications. As already mentioned, this combination uses the strengths of the PHP server capabilities and the dynamic component react architecture to create scalable and high -performance applications. PHP effectively processes server logic, interaction with the database and the creation of the API, and React provides a rich, responsive user interface and uninterrupted data processing.
Xrust How to use php as a backend, and React for front -line
- Если Вам понравилась статья, рекомендуем почитать
- How to use PHP as a backend, and react for front -end
- JavaScript specification updates this year covers many aspects