fix typos and grammar messes in documentation

pull/3/head
maxime.batista 2 years ago
parent e3609f3d0e
commit ca85cc55c4

@ -58,7 +58,7 @@ class SampleFormController {
}
```
As our views are now done using react (and defined under the `front/views` folder), we need to use the `send_react_front($viewURI, $viewArguments)` php function (located in the `src/react-render.php` file).
As our views are now done using react (and defined under the `front/views` folder), we need to use the `send_react_front($viewURI, $viewArguments)` php function (located in the `src/react-render.php` file) to render a react view.
If you look at the `send_react_front($viewURI, $viewArguments)` function, you'll see that is simply loads the file `src/react-display-file.php` with given arguments.
The file is a simple html5 template with a `<script>` block in the `<body>` section.
@ -84,24 +84,27 @@ here's how it renders if you do a request to `http://localhost:8080/`.
![](assets/render-react-php-file-processed.png)
The index.php's router says that for a `GET` on the `/` url, we call the `SampleFormController#displayForm` method.
This method then uses the `send_react_front`, to render the `views/SampleForm.tsx` react element, with no arguments (an empty array).
You can see that the react view is rendered using a `render` function. This function **must figure in the view's file, and be exported**.
We'll talk about an important coding convention about views that this script block implies later.
You can see that the react view is rendered using a `render` function.
This function **must figure in the view's file, and be exported**.
We'll talk about an important coding convention with react views that this script block requires later.
But now let's talk about our server profiles !
## Server Profiles
If you go on the staging server, you'll see that, for the exact same request equivalent, the generated `render-react-file` file changes :
If you go on the staging server, you'll see that, for the exact same request equivalent, the generated `src/render-display-file` file changes :
![](assets/staging-server-render-react-php-file-processed.png)
(we can also see that much less files are downloaded than with our localhost aka development server).
Remember that react components and typescript files needs to be transpiled to javascript before being executable by a browser.
The generated file no longer requests the view to a `localhost:5173` or a `maxou.dev:5173` server,
Now our react components are directly served by the same server, as they has been pre-compiled by our CI (see `/ci/.drone.yml` and `/ci/build_react.msh`) into valid js files that can directly be send to the browser.
If you go back to our `index.php` file, you'll see that it requires a `../config.php` file, if you open it, you'll see that
it defines an `asset(string $uri)` function.
now our react components are directly served by the same server, as they have been pre-compiled by our CI (see `/ci/.drone.yml` and `/ci/build_react.msh`) into valid js files that can directly be send to the browser.
If you go back to our `index.php` file, you'll see that it requires a `../config.php` file, if you open it,
you'll see that it defines the `asset(string $uri)` function that is used by the `src/react-display-file.php`,
in the `<script>` block we talked earlier.
By default, the `/config.php` file uses the `dev-config-profile.php` profile,
the file is replaced with `prod-config-file.php` by the CI when deploying to the staging server (see the pipeline "prepare php" step in `/ci/.drone.yml`)
The two profiles declares a `_asset(string $uri)` function, used by the `/config.php::asset` method, but with different implementations :
The two profiles declares an `_asset(string $uri)` function, used by the `/config.php::asset` method, but with different implementations :
### Development profile
```php
const FRONT_URL_CONSTANT = "http://localhost:5173";
@ -114,14 +117,14 @@ The simplest profile, simply redirect all assets to the development server
### Production profile
Before the CD deploys the generated files to the server,
it'll generate a `/views-mappings.php` file that will map the react views file names to their compiled javascript files :
it generates a `/views-mappings.php` file that will map the react views file names to their compiled javascript files :
```php
const ASSETS = [
// react / typescript path (relative to /front) => its compiled js file name.
'views/SampleForm.tsx' => 'front/views/SampleForm.tsx-82fdeb9a.js',
'views/DisplayResults.tsx' => 'front/views/DisplayResults.tsx-ed098cf4.js',
... // other files that does not have to be directly used by the `send_react_front()` function
'views/SampleForm.tsx' => 'front/views/SampleForm.tsx-82fdeb9a.js',
'views/DisplayResults.tsx' => 'front/views/DisplayResults.tsx-ed098cf4.js',
... // other files that does not have to be directly used by the `send_react_front()` function
];
```
The `_asset` function will then get the right javascript for the given typescript file.
@ -180,7 +183,7 @@ function DisplayResults({password, username}: any) {
}
```
The components takes two arguments, a `username` and `password` string.
The components takes two arguments, an `username` and a `password` string.
Now, let's take a look at its `render` function :
```ts
@ -195,24 +198,10 @@ export function render(args: any) {
);
}
```
let's take a look at this line:
especially this line:
```
<DisplayResults username={args.name} password={args.password} />
```
`DisplayResults` is the name of our React Component (the function).
To pass arguments to the `DisplayResults` function, we use the syntax `<function parameter name>={value}`. (somewhat like regular html/xml components).
Use the render's function `args` parameter to access to the php's array values (see below).
So now we have defined our react view, let's send it to the user's browser from php.
To render the `DisplayResults` view, we'll use the `send_react_front()`:
```php
require_once "react-display.php";
...
send_react_front(
"views/DisplayResults.tsx", // view file location, relative to the `/front` folder.
array("name" => "samuel barion", "password" => "1234") //our function's arguments
)
```
NOTE: in the project, the `$_POST` is placed instead of the hardcoded array, as the `$_POST` is an array containing the "name" and "password" keys. (regarding the SampleForm's form inputs names)).
Use the render's function `args` parameter to access to the php's array values.

@ -1,28 +0,0 @@
# How is react used with php
This document explains how we use react and php together,
and how to create a react component that can be sent to the front end from php.
## Folder tree of the project
```tree
.
├── ci // CI/CD related directory.
├── Documentation // Documentation directory.
├── front // React code goes here.
│ ├── views // React views goes here.
│ └── assets -> // assets goes here (images, svg etc)
├── profiles // PHP server environment profiles.
├── public // index.php goes here
└── src // php code goes here
├── Controller
├── Data
└── ...
```
we'll take a view later on each folder.
## Compilation Constraint
We use typescript and react for the front, which requires to be transpiled to Javascript in order to be executed by browsers.
The fact that our `.tsx` components (our views) needs to be compiled to a js file to be executed does not allows us to dumbly refer to their file path in the php source code.
## Use of ViteJS to build our react components
We use [ViteJS](https://vitejs.dev/guide/) to build the react components.
Loading…
Cancel
Save