Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/riverside/php-express
:horse: PHP micro-framework inspired by Express.js
https://github.com/riverside/php-express
application controller express expressjs micro-framework php php-framework php-router router view viewcontroller web-framework webapp
Last synced: 5 days ago
JSON representation
:horse: PHP micro-framework inspired by Express.js
- Host: GitHub
- URL: https://github.com/riverside/php-express
- Owner: riverside
- License: mit
- Created: 2019-04-15T18:43:00.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-23T10:25:35.000Z (4 months ago)
- Last Synced: 2025-01-09T07:05:04.168Z (12 days ago)
- Topics: application, controller, express, expressjs, micro-framework, php, php-framework, php-router, router, view, viewcontroller, web-framework, webapp
- Language: PHP
- Homepage: https://riverside.github.io/php-express/
- Size: 602 KB
- Stars: 29
- Watchers: 3
- Forks: 11
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# php-express
PHP micro-framework inspired by Express.js| Build | GitHub pages | Stable | License |
| --- | --- | --- | --- |
| [![CI](https://github.com/riverside/php-express/actions/workflows/test.yml/badge.svg)](https://github.com/riverside/php-express/actions/workflows/test.yml) | [![pages-build-deployment](https://github.com/riverside/php-express/actions/workflows/pages/pages-build-deployment/badge.svg)](https://github.com/riverside/php-express/actions/workflows/pages/pages-build-deployment) | [![Latest Stable Version](https://poser.pugx.org/riverside/php-express/v/stable)](https://packagist.org/packages/riverside/php-express) | [![License](https://poser.pugx.org/riverside/php-express/license)](https://packagist.org/packages/riverside/php-express) |### Requirements
- PHP >= 7.1
- PHP extensions:
- JSON (`ext-json`)### Installation
If Composer is not installed on your system yet, you may go ahead and install it using this command line:
```
$ curl -sS https://getcomposer.org/installer | php
```
Next, add the following require entry to thecomposer.json
file in the root of your project.
```json
{
"require" : {
"riverside/php-express" : "^2.0"
}
}
```
Finally, use Composer to install php-express and its dependencies:
```
$ php composer.phar install
```
### Routing
```php
get('/', function ($req, $res) {
$res->send('hello world');
});
```
#### Route methods
```php
get('/', function ($req, $res) {
$res->send('GET request to the homepage');
});// POST method route
$app->post('/', function ($req, $res) {
$res->send('POST request to the homepage');
});
```
#### Route paths
```php
get('/', function ($req, $res) {
$res->send('root');
});$app->get('about', function ($req, $res) {
$res->send('about');
});$app->get('random.text', function ($req, $res) {
$res->send('random.text');
});
```#### Response methods
| Method | Description |
| ------------------ | --------------------------------- |
| $res->end() | End the response process. |
| $res->json() | Send a JSON response. |
| $res->redirect() | Redirect a request. |
| $res->render() | Render a view template. |
| $res->send() | Send a response of various types. |
| $res->sendStatus() | Set the response status code and send its string representation as the response body. |#### $app->route()
```php
route('/book')
->get(function ($req, $res) {
$res->send('Get a random book');
})
->post(function ($req, $res) {
$res->send('Add a book');
})
->put(function ($req, $res) {
$res->send('Update the book');
});
```#### Router
```php
param('uuid', '[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}');$router->get('/', function ($req, $res) {
$res->send('Birds home page');
});$router->get('about', function ($req, $res) {
$res->send('About birds');
});$router->get('ticket/:uuid/', function($req, $res) {
echo $req->params['uuid'];
});$router->run();
```
#### Middleware
```php
$app->use(function($req, $res) {
$res->header('X-Frame-Options', 'DENY');
$res->header('X-Powered-By', false);
});$app->use('/cors', function($req, $res) {
$res->header('Access-Control-Allow-Origin', '*');
});
```