Skip to content

Commit 2b8fa74

Browse files
committed
'Solution'
1 parent 7eafc66 commit 2b8fa74

12 files changed

Lines changed: 842 additions & 119 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24+
25+
# added by Bryan
26+
/.idea

README.md

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,39 @@
1-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
1+
Bryan's first React webapp with PHP endpoints
2+
=============================================
23

3-
## Available Scripts
4+
Installation to run in production
5+
---------------------------------
46

5-
In the project directory, you can run:
7+
1. Extract contents
68

7-
### `npm start`
9+
2. Install `npm` modules automatically based on `package.json`
810

9-
Runs the app in the development mode.<br />
10-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11+
cd helloreact
12+
npm install
13+
14+
3. Build the React client-side
1115

12-
The page will reload if you make edits.<br />
13-
You will also see any lint errors in the console.
16+
npm run build
17+
18+
4. Copy the contents of `helloreact/build` directly into the `htdocs` of your XAMPP or other PHP server-side environment.
1419

15-
### `npm test`
20+
5. Create the MySQL database `helloreact` and user account, with matching password as found in the source code of the `build/endpoints` PHP endpoints.
1621

17-
Launches the test runner in the interactive watch mode.<br />
18-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
22+
create database helloreact;
23+
grant all on helloreact.* to helloreact@localhost identified by 'EoJe0jJWXsAe';
24+
25+
All necessary tables will be automatically created when creating your first article in the webapp.
1926

20-
### `npm run build`
27+
Installation for development purposes
28+
-------------------------------------
2129

22-
Builds the app for production to the `build` folder.<br />
23-
It correctly bundles React in production mode and optimizes the build for the best performance.
30+
For real-time development of the React client-side without having to re-run `npm run build` on every change you will need to segregate the PHP server-side so that you can use the out-of-the-box React server-side for development of the client-side.
2431

25-
The build is minified and the filenames include the hashes.<br />
26-
Your app is ready to be deployed!
32+
1. Copy the `public/endpoints` directory to your XAMPP or other PHP server-side environment.
2733

28-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
34+
2. Edit the PHP files on your PHP server-side copy and uncomment the `Access-Control-Allow-Origin` line in each. This will allow requests from the origin of your temporary/development React server-side to gain access to the PHP server-side endpoints.
2935

30-
### `npm run eject`
36+
3. Within `src/App.js`, uncomment the second `var phpEndpoints` line and specify the alternative origin of the PHP endpoints.
3137

32-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
38+
4. `npm run start` will start the React client-side and, if all goes according to plan, it links right up to the origin of your PHP server-side.
3339

34-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35-
36-
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37-
38-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39-
40-
## Learn More
41-
42-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43-
44-
To learn React, check out the [React documentation](https://reactjs.org/).
45-
46-
### Code Splitting
47-
48-
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49-
50-
### Analyzing the Bundle Size
51-
52-
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53-
54-
### Making a Progressive Web App
55-
56-
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57-
58-
### Advanced Configuration
59-
60-
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61-
62-
### Deployment
63-
64-
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65-
66-
### `npm run build` fails to minify
67-
68-
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify

package-lock.json

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@testing-library/user-event": "^7.2.1",
99
"react": "^16.12.0",
1010
"react-dom": "^16.12.0",
11+
"react-router-dom": "^5.1.2",
1112
"react-scripts": "3.3.1"
1213
},
1314
"scripts": {

public/.htaccess

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Options +FollowSymLinks
2+
RewriteEngine on
3+
RewriteRule ^add$ index.html [L]
4+
RewriteRule ^\d+$ index.html [L]
5+
RewriteRule ^\d+/edit$ index.html [L]

public/endpoints/fetch-list.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
//header('Access-Control-Allow-Origin: http://localhost:3000'); // for development purposes only
4+
5+
header('Cache-Control: no-cache');
6+
7+
$servername = "localhost";
8+
$username = "helloreact";
9+
$password = "EoJe0jJWXsAe";
10+
$dbname = "helloreact";
11+
12+
$conn = new mysqli($servername, $username, $password, $dbname);
13+
// Check connection
14+
if ($conn->connect_error) {
15+
die("Connection failed: " . $conn->connect_error);
16+
}
17+
18+
$stmt = $conn->prepare("SELECT id, title from article");
19+
header('Content-Type: application/json');
20+
if ($stmt) {
21+
$stmt->execute();
22+
$result = $stmt->get_result();
23+
24+
// output data of each row
25+
$inc = 0;
26+
echo "{";
27+
while($row = $result->fetch_assoc()) {
28+
if ($inc++) { echo ","; }
29+
echo '"' . $row['id'] . '":';
30+
echo "{";
31+
echo '"id":' . $row['id'];
32+
echo ',"title":' . json_encode($row["title"]);
33+
echo "}";
34+
}
35+
echo "}";
36+
} else {
37+
if ($conn->errno != 1146) { die; } // expect table doesn't exists
38+
echo "{}";
39+
}
40+
41+
$conn->close();
42+
43+
44+
?>

public/endpoints/fetch-one.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
//header('Access-Control-Allow-Origin: http://localhost:3000'); // for development purposes only
4+
5+
header('Cache-Control: no-cache');
6+
7+
$servername = "localhost";
8+
$username = "helloreact";
9+
$password = "EoJe0jJWXsAe";
10+
$dbname = "helloreact";
11+
12+
$conn = new mysqli($servername, $username, $password, $dbname);
13+
// Check connection
14+
if ($conn->connect_error) {
15+
die("Connection failed: " . $conn->connect_error);
16+
}
17+
18+
$stmt = $conn->prepare("SELECT id, title, content from article where id=?");
19+
if ($stmt) {
20+
$id = substr($_SERVER['PATH_INFO'], 1);
21+
$stmt->bind_param("s", $id);
22+
$stmt->execute();
23+
$result = $stmt->get_result();
24+
25+
// output data of each row
26+
$inc = 0;
27+
if($row = $result->fetch_assoc()) {
28+
header('Content-Type: application/json');
29+
echo "{";
30+
echo '"id":' . $row['id'];
31+
echo ',"title":' . json_encode($row["title"]);
32+
echo ',"content":' . json_encode($row["content"]);
33+
echo "}";
34+
} else {
35+
http_response_code(410);
36+
header('Content-Type: text/plain');
37+
echo "This article does not exist.";
38+
}
39+
} else {
40+
if ($conn->errno != 1146) { die; } // expect table doesn't exists
41+
http_response_code(410);
42+
header('Content-Type: text/plain');
43+
echo "No articles have been created yet.";
44+
}
45+
46+
$conn->close();
47+
48+
49+
?>

0 commit comments

Comments
 (0)