PHP code example of tbht / press
1. Go to this page and download the library: Download tbht/press library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tbht / press example snippets
use Press\Application;
use Press\Context;
$app = new Application();
$app->use(function (Context $ctx, callable $next) {
$ctx->body = 'Hello Press';
});
$app->listen();
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");
});
});
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]);
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';
});
use Press\Application;
$app = new Application();
$app->listen([
"host" => "127.0.0.1",
"port" => 8080
]);
// or
$app->listen(function () {
echo "call back run";
});
use React\EventLoop\Factory;
$loop = Factory::create();
$server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
// ...
});
$app->use(function ($ctx) {
$ctx->db = new DB();
});
$app->on("error", function () {
echo "this is an error";
});