Skip to content

Commit 1d4062a

Browse files
authored
Merge pull request #158 from kethinov/docs-update
docs update
2 parents 56083dc + fada984 commit 1d4062a

4 files changed

Lines changed: 97 additions & 103 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# Changelog
2-
3-
## Next version
4-
5-
- Put your changes here...
6-
71
## 1.1.0
82

93
- Added `run` and `runWithData` methods so this module can be used as a general purpose PHP runner.

CONFIGURATION.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
This module will register values from the data model you pass to the PHP script as global variables in your PHP script by default when you use PHP as an Express view engine or when you call `runWithData`. You can disable this behavior if desired in the following ways:
2+
3+
Disable registering globally:
4+
5+
```js
6+
const php = require('php')
7+
php.disableRegisterGlobalModel()
8+
// can be reenabled by calling php.enableRegisterGlobalModel()
9+
```
10+
11+
Disable registering on a per render basis in Express:
12+
13+
```js
14+
app.get('/', (req, res) => {
15+
res.render('index.php', {
16+
_REGISTER_GLOBAL_MODEL: false,
17+
hello: 'world'
18+
})
19+
})
20+
```
21+
22+
Disable registering on a per render basis in `runWithData` (though if you're doing this, you probably should just use `php.run()` instead, as that method was written to use simpler logic that doesn't support passing data to PHP):
23+
24+
```js
25+
const output = await php.runWithData('some_php_script.php', {
26+
_REGISTER_GLOBAL_MODEL: false,
27+
hello: 'world'
28+
})
29+
```

README.md

Lines changed: 9 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,17 @@
1-
# node-php-runner
2-
3-
[![Build Status](https://github.com/rooseveltframework/express-php-view-engine/workflows/CI/badge.svg
4-
)](https://github.com/rooseveltframework/express-php-view-engine/actions?query=workflow%3ACI) [![npm](https://img.shields.io/npm/v/php.svg)](https://www.npmjs.com/package/php)
1+
[![npm](https://img.shields.io/npm/v/php.svg)](https://www.npmjs.com/package/php)
52

63
This module allows you to run [PHP](https://php.net) code in Node.js in various ways:
74

85
- Run PHP scripts.
96
- Run PHP scripts and pass them JSON data from Node.js.
107
- Use PHP as a view engine (templating system) for [Express framework](https://expressjs.com) applications.
118

12-
To use this module, you must have PHP installed and in your PATH.
13-
14-
This module was built and is maintained by the [Roosevelt web framework](https://github.com/rooseveltframework/roosevelt) [team](https://github.com/orgs/rooseveltframework/people), but it can be used independently of Roosevelt as well.
15-
16-
## Run a PHP script in Node.js
17-
18-
```javascript
19-
const php = require('php')
20-
const output = await php.run('some_php_script.php')
21-
```
22-
23-
## Run a PHP script in Node.js and pass it data
24-
25-
```javascript
26-
const php = require('php')
27-
const output = await php.runWithData('some_php_script.php', { hello: 'world' })
28-
```
29-
30-
Then, assuming your `some_php_script.php` file looks like this:
31-
32-
```php
33-
<p><?=$hello?></p>
34-
```
35-
36-
The output will be:
37-
38-
```html
39-
<p>world</p>
40-
```
41-
42-
## Use with Express
43-
44-
```js
45-
const express = require('express')
46-
const app = express()
47-
const php = require('php')
48-
49-
// setup PHP templating engine
50-
app.set('views', path.join(__dirname, 'templates'))
51-
app.set('view engine', 'php') // set PHP as a view engine in your Express app
52-
app.engine('php', php.__express)
53-
54-
// define a route
55-
app.get('/', (req, res) => {
56-
res.render('index.php', {
57-
hello: 'world'
58-
})
59-
})
60-
```
61-
62-
Then, assuming your `templates/index.php` looks like this:
63-
64-
```php
65-
<p><?=$hello?></p>
66-
```
67-
68-
The output will be:
69-
70-
```html
71-
<p>world</p>
72-
```
73-
74-
## Configuration
75-
76-
As shown in the above examples, this module will register values from the data model you pass to the PHP script as global variables in your PHP script by default when you use PHP as an Express view engine or when you call `runWithData`. You can disable this behavior if desired in the following ways:
77-
78-
Disable registering globally:
79-
80-
```js
81-
const php = require('php')
82-
php.disableRegisterGlobalModel()
83-
// can be reenabled by calling php.enableRegisterGlobalModel()
84-
```
85-
86-
Disable registering on a per render basis in Express:
87-
88-
```js
89-
app.get('/', (req, res) => {
90-
res.render('index.php', {
91-
_REGISTER_GLOBAL_MODEL: false,
92-
hello: 'world'
93-
})
94-
})
95-
```
96-
97-
Disable registering on a per render basis in `runWithData` (though if you're doing this, you probably should just use `php.run()` instead, as that method was written to use simpler logic that doesn't support passing data to PHP):
98-
99-
```js
100-
const output = await php.runWithData('some_php_script.php', {
101-
_REGISTER_GLOBAL_MODEL: false,
102-
hello: 'world'
103-
})
104-
```
9+
This module was built and is maintained by the [Roosevelt web framework](https://rooseveltframework.org) [team](https://rooseveltframework.org/contributors), but it can be used independently of Roosevelt as well.
10510

11+
<details open>
12+
<summary>Documentation</summary>
13+
<ul>
14+
<li><a href="./USAGE.md">Usage</a></li>
15+
<li><a href="./CONFIGURATION.md">CONFIGURATION</a></li>
16+
</ul>
17+
</details>

USAGE.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
To use this module, you must have PHP installed and in your PATH.
2+
3+
## Run a PHP script in Node.js
4+
5+
```javascript
6+
const php = require('php')
7+
const output = await php.run('some_php_script.php')
8+
```
9+
10+
## Run a PHP script in Node.js and pass it data
11+
12+
```javascript
13+
const php = require('php')
14+
const output = await php.runWithData('some_php_script.php', { hello: 'world' })
15+
```
16+
17+
Then, assuming your `some_php_script.php` file looks like this:
18+
19+
```php
20+
<p><?=$hello?></p>
21+
```
22+
23+
The output will be:
24+
25+
```html
26+
<p>world</p>
27+
```
28+
29+
## Use with Express
30+
31+
```js
32+
const express = require('express')
33+
const app = express()
34+
const php = require('php')
35+
36+
// setup PHP templating engine
37+
app.set('views', path.join(__dirname, 'templates'))
38+
app.set('view engine', 'php') // set PHP as a view engine in your Express app
39+
app.engine('php', php.__express)
40+
41+
// define a route
42+
app.get('/', (req, res) => {
43+
res.render('index.php', {
44+
hello: 'world'
45+
})
46+
})
47+
```
48+
49+
Then, assuming your `templates/index.php` looks like this:
50+
51+
```php
52+
<p><?=$hello?></p>
53+
```
54+
55+
The output will be:
56+
57+
```html
58+
<p>world</p>
59+
```

0 commit comments

Comments
 (0)