ahmard / php-server
A small library to help run PHP servers easily and quickly.
0.0.3
2021-11-18 13:09 UTC
Requires
- php: ^8.0
- ext-pdo: *
- ahmard/quick-route: ^3.8
- nette/utils: ^3.2
- opis/closure: ^3.6
- react/child-process: ^0.6.3
- react/http: ^1.5
- symfony/console: ^5.3
- vlucas/phpdotenv: ^5.3
Requires (Dev)
- phpstan/phpstan: ^0.12.98
- swoole/ide-helper: ^4.7
- symfony/var-dumper: ^5.3
This package is auto-updated.
Last update: 2022-05-18 14:12:26 UTC
README
A small library to help run PHP servers easily and quickly.
Installation
composer require ahmard/php-server
Usage
PHP Built-In Server
An implementation of Built-In Server
- With document root
use PHPServer\BuiltIn\Server; Server::create('127.0.0.1', '9900') ->setDocumentRoot(__DIR__) ->start() ->logOutputToConsole();
- Route request to single entry file
use PHPServer\BuiltIn\Server; Server::create('127.0.0.1', '9900') ->setRouterScript(__DIR__ . 'public/index.php') ->start();
- Provide callable to be invoked when request is received
use PHPServer\BuiltIn\Server; Server::create('127.0.0.1', '9900') ->onRequest(fn() => var_dump('Request Received')) ->start();
ReactPHP
An implementation of ReactPHP
use PHPServer\React\Server; use Psr\Http\Message\RequestInterface; use React\Http\Message\Response; require 'vendor/autoload.php'; $handler = function (RequestInterface $request) { $html = 'Welcome,<br/>'; $html .= "Method: {$request->getMethod()}<br/>"; $html .= "Route: {$request->getUri()->getPath()}"; return new Response(200, ['Content-Type' => 'text/html'], $html); }; Server::create('127.0.0.1', 9001) ->onRequest($handler) ->start() ->logOutputToConsole();
Swoole
An implementation of Swoole
use PHPServer\Swoole\Http\Request; use PHPServer\Swoole\Server; require 'vendor/autoload.php'; $handler = function (Request $request) { $html = 'Welcome,<br/>'; $html .= "Method: {$request->getMethod()}<br/>"; $html .= "Route: {$request->getUri()->getPath()}"; $request->response()->html($html); }; Server::create('127.0.0.1', 9904) ->onRequest($handler) ->setServerConfig([ 'enable_static_handler' => true, 'http_parse_post' => true, 'worker_num' => 8, 'package_max_length' => 10 * 1024 * 1024 ]) ->start() ->logOutputToConsole();
Swoole with filesystem watcher, this requires an installation of ahmard/swotch package.
use PHPServer\Swoole\Http\Request; use PHPServer\Swoole\Server; require 'vendor/autoload.php'; Server::create('127.0.0.1', 9904) ->watchFilesystemChanges([__DIR__]) ->onRequest(function (Request $request){ $request->response()->html('Hello'); }) ->setServerConfig([ 'enable_static_handler' => true, 'http_parse_post' => true, 'worker_num' => 8, 'package_max_length' => 10 * 1024 * 1024 ]) ->start() ->logOutputToConsole();