https://github.com/tbht/press
An async framework in php, like koa in nodejs
https://github.com/tbht/press
callback middleware php php7 reactphp
Last synced: 2 months ago
JSON representation
An async framework in php, like koa in nodejs
- Host: GitHub
- URL: https://github.com/tbht/press
- Owner: TbhT
- License: apache-2.0
- Created: 2018-06-06T14:16:00.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-07-21T13:21:54.000Z (almost 4 years ago)
- Last Synced: 2025-12-14T21:46:56.583Z (6 months ago)
- Topics: callback, middleware, php, php7, reactphp
- Language: PHP
- Homepage:
- Size: 464 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# Press

[](https://codecov.io/gh/TbhT/press)
> This project is a translation of koa framework of nodejs.
## Installation
`$ composer require tbht/press`
## Hello `Press`
```php
use Press\Application;
use Press\Context;
$app = new Application();
$app->use(function (Context $ctx, callable $next) {
$ctx->body = 'Hello Press';
});
$app->listen();
```
## Middleware
Here is an example of logger middleware :
```php
use Press\Context;
use Press\Application;
$app = new Application();
$app->use(function (Context $ctx, callable $next) {
$start = time();
return $next()
->then(function () use($start, $ctx) {
$ms = time() - $start;
$method = $ctx->method;
$url = $ctx->url;
print_r("{$method} {$url} - {$ms}ms");
});
});
```
## Document
### Application
#### hello world
```php
use Press\Application;
use Press\Context;
$app = new Application();
$app->use(function (Context $ctx, callable $next) {
$ctx->body = 'Hello World';
});
$app->listen(function () {
var_dump('final var dump');
});
// or
$app->listen(['port' => 8080]);
```
#### Cascading
```php
use Press\Application;
use Press\Context;
$app = new Application();
// logger
$app->use(function (Context $ctx, callable $next) {
return $next()
->then(function () use ($ctx) {
$rt = $ctx->response->get('x-response-time');
$method = $ctx->method;
$url = $ctx->url;
echo "{$method} {$url} - {$rt}";
});
});
// x-response-time
$app->use(function (Context $ctx, callable $next) {
$start = time();
return $next()
->then(function () use ($ctx,$start) {
$ms = time() - $start;
$ctx->set('x-response-time', "{$ms}ms");
});
});
// response
$app->use(function (Context $ctx, callable $next) {
$ctx->body = 'Hello World';
});
```
#### Settings
- `$app->env` default to the 'development'
- `$app->proxy` when true proxy header fields will be trusted
- `$app->subdomainOffset` offset of `.subdomains` to ignore [2]
#### $app->listen(...)
```php
use Press\Application;
$app = new Application();
$app->listen([
"host" => "127.0.0.1",
"port" => 8080
]);
// or
$app->listen(function () {
echo "call back run";
});
```
#### $app->callback()
return a callback function suitable for the following http server request.
```php
use React\EventLoop\Factory;
$loop = Factory::create();
$server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
// ...
});
```
### $app->use()
see `Middlware` part
### $app->context
`$app->context` is the prototype from which `ctx` is created.
```php
$app->use(function ($ctx) {
$ctx->db = new DB();
});
```
### Error handling
```php
$app->on("error", function () {
echo "this is an error";
});
```
## Context