M4W Framework
Create an application
composer create-project mini4work/framework my-app
cd my-app
composer require mini4work/webserverApplication service
Save this class in app/Services/RequestInfo.php. Its constructor asks the framework container for the current request.
<?php
namespace App\Services;
use M4W\WebServer\Http\Request;
final class RequestInfo
{
public function __construct(private Request $request) {}
public function describe(): array
{
return ['method' => $this->request->method, 'uri' => $this->request->uri];
}
}Write a handler
Save this as server.php next to m4w. Each request gets its own container. Register the request before resolving the service.
<?php
use App\Services\RequestInfo;
use M4W\Core\Framework;
use M4W\WebServer\Cli\ServerCommand;
use M4W\WebServer\Http\CallbackRequestHandler;
use M4W\WebServer\Http\Request;
use M4W\WebServer\Http\Response;
require __DIR__.'/vendor/autoload.php';
$handler = new CallbackRequestHandler(static function (Request $request): Response {
$app = new Framework();
$app->singleton(Request::class, $request);
return Response::json($app->make(RequestInfo::class)->describe());
});
exit((new ServerCommand)->run($argv, $handler));Start the server
Save this as m4w.json next to server.php.
{
"listen": { "port": 9000 }
}php server.php m4w.jsonIn a second terminal, send a request to the loopback listener.
curl -i 'http://127.0.0.1:9000/inspect?q=hello'{"method": "GET", "uri": "/inspect?q=hello"}