Skip to content

Instantly share code, notes, and snippets.

@nixoncode
Created October 8, 2020 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixoncode/3fbca0f710296c0b00f0639925e88442 to your computer and use it in GitHub Desktop.
Save nixoncode/3fbca0f710296c0b00f0639925e88442 to your computer and use it in GitHub Desktop.
<?php
use DI\ContainerBuilder;
use Slim\Http\Response;
use DI\Bridge\Slim\Bridge;
use Spiral\Goridge\StreamRelay;
use Spiral\RoadRunner\PSR7Client;
ini_set('display_errors', 'stderr');
require_once __DIR__ . './../vendor/autoload.php';
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions(
[
'name' => 'User',
]
);
$container = $containerBuilder->build();
$app = Bridge::create($container);
//add a route handler for /
// [{name}] defines an optional param for the route
$app->get('/[{name}]', function ( Response $response, $name = null) use ($app) {
if (!$name){ // name was not supplied, use the one in the container
$name = $app->getContainer()->get('name');
}
$response->getBody()->write("Hello $name");
return $response;
}
);
$relay = new StreamRelay(STDIN, STDOUT);
$psr7 = new PSR7Client(new \Spiral\RoadRunner\Worker($relay));
while ($req = $psr7->acceptRequest()) {
try {
//runs the app silently
$response = $app->handle($req);
//pass the response back to roadrunner psr client
$psr7->respond($response);
} catch (Throwable $exception) {
$psr7->getWorker()->error((string)$exception);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment