|
1 | | -# php |
2 | | -PHP stackable for Watt |
| 1 | +# @platformatic/php |
| 2 | + |
| 3 | +A PHP stackable for [Platformatic](https://platformatic.dev/) that enables running PHP applications within the Platformatic ecosystem. This package integrates PHP execution with Fastify servers, allowing you to serve PHP files alongside Node.js applications. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🚀 Run PHP applications within Platformatic services |
| 8 | +- 🔄 Automatic request/response handling between Node.js and PHP |
| 9 | +- 📁 Static file serving for non-PHP assets |
| 10 | +- ⚡ Hot reloading during development |
| 11 | +- 🛠️ Code generation for new PHP projects |
| 12 | +- 🔧 Environment-based configuration |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +- Node.js >= 22.14.0 |
| 17 | +- The PHP runtime is built thanks to [`@platformatic/php-node`](https://github.com/platformatic/php-node). |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install @platformatic/php |
| 23 | +``` |
| 24 | + |
| 25 | +## Quick Start |
| 26 | + |
| 27 | +### Create a New PHP Project |
| 28 | + |
| 29 | +```bash |
| 30 | +npx --package=@platformatic/php create-platformatic-php --dir my-php-app --port 3042 |
| 31 | +cd my-php-app |
| 32 | +npm install |
| 33 | +npm start |
| 34 | +``` |
| 35 | + |
| 36 | +### CLI Options |
| 37 | + |
| 38 | +- `--dir` - Target directory (default: `plt-php`) |
| 39 | +- `--port` - Server port (default: `3042`) |
| 40 | +- `--hostname` - Server hostname (default: `0.0.0.0`) |
| 41 | +- `--main` - Main PHP file (default: `index.php`) |
| 42 | + |
| 43 | +## Configuration |
| 44 | + |
| 45 | +The stackable uses a `platformatic.json` configuration file: |
| 46 | + |
| 47 | +```json |
| 48 | +{ |
| 49 | + "$schema": "https://schemas.platformatic.dev/@platformatic/php/0.4.3.json", |
| 50 | + "module": "@platformatic/php", |
| 51 | + "php": { |
| 52 | + "docroot": "public" |
| 53 | + }, |
| 54 | + "server": { |
| 55 | + "hostname": "{PLT_SERVER_HOSTNAME}", |
| 56 | + "port": "{PORT}", |
| 57 | + "logger": { "level": "{PLT_SERVER_LOGGER_LEVEL}" } |
| 58 | + }, |
| 59 | + "watch": true |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### Configuration Options |
| 64 | + |
| 65 | +#### php |
| 66 | +- `docroot` (string, required) - Path to the root directory containing PHP files |
| 67 | + |
| 68 | +#### server |
| 69 | +Standard Platformatic server configuration options are supported. |
| 70 | + |
| 71 | +## Project Structure |
| 72 | + |
| 73 | +A generated PHP project includes: |
| 74 | + |
| 75 | +``` |
| 76 | +my-php-app/ |
| 77 | +├── public/ |
| 78 | +│ └── index.php # Main PHP file |
| 79 | +├── .env # Environment variables |
| 80 | +├── .env.sample # Environment template |
| 81 | +├── .gitignore |
| 82 | +├── package.json |
| 83 | +└── platformatic.json # Platformatic configuration |
| 84 | +``` |
| 85 | + |
| 86 | +## Development |
| 87 | + |
| 88 | +### Available Scripts |
| 89 | + |
| 90 | +- `npm start` - Start the development server |
| 91 | +- `npm test` - Run tests |
| 92 | +- `npm run build` - Build schema and types |
| 93 | + |
| 94 | +### Environment Variables |
| 95 | + |
| 96 | +- `PLT_SERVER_HOSTNAME` - Server hostname (default: `0.0.0.0`) |
| 97 | +- `PORT` - Server port (default: `3042`) |
| 98 | +- `PLT_SERVER_LOGGER_LEVEL` - Log level (default: `info`) |
| 99 | + |
| 100 | +## How It Works |
| 101 | + |
| 102 | +1. **Request Routing**: All HTTP requests are captured by wildcard routes |
| 103 | +2. **PHP Execution**: Requests are forwarded to PHP via `@platformatic/php-node` |
| 104 | +3. **Static Files**: Non-PHP files in the docroot are served statically |
| 105 | +4. **Response Handling**: PHP responses are processed and returned through Fastify |
| 106 | + |
| 107 | +## API |
| 108 | + |
| 109 | +### Stackable Export |
| 110 | + |
| 111 | +```javascript |
| 112 | +import { stackable } from '@platformatic/php' |
| 113 | +// or |
| 114 | +import php from '@platformatic/php' |
| 115 | +``` |
| 116 | + |
| 117 | +### Generator |
| 118 | + |
| 119 | +```javascript |
| 120 | +import { Generator } from '@platformatic/php' |
| 121 | + |
| 122 | +const generator = new Generator() |
| 123 | +generator.setConfig({ |
| 124 | + targetDirectory: './my-app', |
| 125 | + port: 3042, |
| 126 | + hostname: '0.0.0.0' |
| 127 | +}) |
| 128 | +await generator.run() |
| 129 | +``` |
| 130 | + |
| 131 | +## Examples |
| 132 | + |
| 133 | +### Basic PHP Application |
| 134 | + |
| 135 | +```php |
| 136 | +<?php |
| 137 | +// public/index.php |
| 138 | +header("Content-Type: application/json"); |
| 139 | +echo json_encode([ |
| 140 | + "message" => "Hello from PHP!", |
| 141 | + "timestamp" => date('c') |
| 142 | +]); |
| 143 | +?> |
| 144 | +``` |
| 145 | + |
| 146 | +### Handling POST Requests |
| 147 | + |
| 148 | +```php |
| 149 | +<?php |
| 150 | +// public/api.php |
| 151 | +if ($_SERVER['REQUEST_METHOD'] === 'POST') { |
| 152 | + $input = json_decode(file_get_contents('php://input'), true); |
| 153 | + |
| 154 | + header("Content-Type: application/json"); |
| 155 | + echo json_encode([ |
| 156 | + "received" => $input, |
| 157 | + "method" => $_SERVER['REQUEST_METHOD'] |
| 158 | + ]); |
| 159 | +} |
| 160 | +?> |
| 161 | +``` |
| 162 | + |
| 163 | +## Contributing |
| 164 | + |
| 165 | +This project is part of the [Platformatic](https://github.com/platformatic) ecosystem. Please refer to the main repository for contribution guidelines. |
| 166 | + |
| 167 | +## License |
| 168 | + |
| 169 | +Apache-2.0 |
| 170 | + |
| 171 | +## Support |
| 172 | + |
| 173 | +- [GitHub Issues](https://github.com/platformatic/php/issues) |
| 174 | +- [Platformatic Documentation](https://docs.platformatic.dev/) |
| 175 | +- [Community Discord](https://discord.gg/platformatic) |
0 commit comments