Yii3
Run one Yii application per worker. Pass a factory for a PSR container that provides Application and StateResetter. Yii starts on the first request.
Install the package
composer require 'mini4work/webserver:^1.8' 'yiisoft/yii-http:^1.1' 'yiisoft/di:^1.5' 'nyholm/psr7:^1.8'Write a handler
Save this as server.php next to vendor/.
<?php
declare(strict_types=1);
use M4W\WebServer\Adapters\Yii\YiiRequestHandler;
use M4W\WebServer\Cli\ServerCommand;
use Nyholm\Psr7\Response;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory;
use Yiisoft\Yii\Http\Application;
require __DIR__.'/vendor/autoload.php';
$handler = new YiiRequestHandler(static fn () => new Container(ContainerConfig::create()->withDefinitions([
Application::class => static function (ContainerInterface $container) {
$dispatcher = new MiddlewareDispatcher(new MiddlewareFactory($container));
return new Application($dispatcher->withMiddlewares([
static function (ServerRequestInterface $request) {
if ($request->getUri()->getPath() !== '/hello') {
return new Response(404);
}
$name = $request->getCookieParams()['name'] ?? null;
return new Response(body: 'Hello '.(is_string($name) ? $name : 'guest'));
},
]));
},
])));
exit((new ServerCommand)->run($argv ?? [__FILE__], $handler));Start the server
Save this as m4w.json next to server.php.
{
"listen": { "port": 9000 },
"protocols": { "http": "1.1" },
"tls": { "mode": "off" },
"workers": { "count": 1 }
}php server.php m4w.jsonSend a request from a second terminal.
curl -i -X POST --cookie 'name=Ada' http://127.0.0.1:9000/helloHTTP/1.1 200 OK · Hello Ada
Request state
Yii uses the default PSR body and upload limits. A slow response holds its worker’s Yii container until the body closes.
Overlapping requests wait in arrival order. The worker admits the next request after the body closes, afterEmit() runs and StateResetter resets registered services. Register reset callbacks for request-specific state. Failed dispatch calls afterEmit(null). Normal worker shutdown calls shutdown().
Set queueTimeout in seconds to a positive finite value (default 5). On expiry Yii returns 503 unless the transport already cancelled the request. HTTP/2 streams on one connection share a worker. Cancelling a waiter releases its place. HTTP/3 jobs use the application process pool.
new YiiRequestHandler($containerFactory, queueTimeout: 2.0);