PHP code example of york8 / poa-static
1. Go to this page and download the library: Download york8/poa-static 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/ */
york8 / poa-static example snippets
$app = new Application();
$staticMiddleware = new StaticMiddleware('..');
$staticMiddleware->use(new ProfileMiddleware())->use(new SimpleFilenamePlugin('/static'));
// 定义路由器,将路径 /static 前缀开头的请求交给 StaticMiddleware 处理
$router = new RouterMiddleware(function (Context $context) {
$context->statusCode(404)->send('Not Found');
});
$router->get('/static', $staticMiddleware);
$app->useErrorMiddleware(function (Throwable $throwable) {
// 全局未捕获 错误/异常 处理
fwrite(STDERR, $throwable->getMessage() . "\n");
})
// 简单请求错误异常处理
->use(function (Context $context) {
try {
yield;
} catch (Throwable $exception) {
fwrite(STDERR, $exception->getMessage() . "\n");
$context->statusCode(500)->send($exception->getMessage());
}
})
// 使用路由器
->use($router);
$app->listen('0.0.0.0:8088');