Non blocking, async and +/-30% faster solution than apache, plus much thinner docker containers. These are the main, most important pros (at least for me). For an isolated network (containers ports not published to the host machine) looks like a very interesting solution:
With a small hack:
src/App.php
<?php
namespace App;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Factory\ServerRequestCreatorFactory;
use Psr\Http\Message\ResponseInterface as Response;
class App extends \Slim\App
{
/**
* @var Response;
*/
public static $response; // <= (1)
/**
* Run application
*
* This method traverses the application middleware stack and then sends the
* resultant Response object to the HTTP client.
*
* @param ServerRequestInterface|null $request
* @return void
*/
public function run(?ServerRequestInterface $request = null): void
{
if (!$request) {
$serverRequestCreator = ServerRequestCreatorFactory::create();
$request = $serverRequestCreator->createServerRequestFromGlobals();
}
self::$response = $this->handle($request); // <= (2)
}
}
src/AppFactory.php
<?php
namespace App;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\MiddlewareDispatcherInterface;
use Slim\Interfaces\RouteCollectorInterface;
use Slim\Interfaces\RouteResolverInterface;
class AppFactory extends \Slim\Factory\AppFactory // <= (3)
{
/**
* @param ResponseFactoryInterface|null $responseFactory
* @param ContainerInterface|null $container
* @param CallableResolverInterface|null $callableResolver
* @param RouteCollectorInterface|null $routeCollector
* @param RouteResolverInterface|null $routeResolver
* @param MiddlewareDispatcherInterface|null $middlewareDispatcher
* @return App
*/
public static function create(
?ResponseFactoryInterface $responseFactory = null,
?ContainerInterface $container = null,
?CallableResolverInterface $callableResolver = null,
?RouteCollectorInterface $routeCollector = null,
?RouteResolverInterface $routeResolver = null,
?MiddlewareDispatcherInterface $middlewareDispatcher = null
): App {
static::$responseFactory = $responseFactory ?? static::$responseFactory;
return new App(
self::determineResponseFactory(),
$container ?? static::$container,
$callableResolver ?? static::$callableResolver,
$routeCollector ?? static::$routeCollector,
$routeResolver ?? static::$routeResolver,
$middlewareDispatcher ?? static::$middlewareDispatcher
);
}
}
And simple server:
$ php server.php
server.php
<?php
require 'vendor/autoload.php';
use App\AppFactory;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use React\EventLoop\Loop;
use React\Http\Server as HttpServer;
use React\Socket\Server as SocketServer;
$loop = Loop::get();
$server = new HttpServer(function (Request $request) {
echo 'Request to ' . $request->getUri(). PHP_EOL;
$app = AppFactory::create();
$app->get('/dudu', function (Request $request, Response $response, $args) {
$response->getBody()->write("Hello Baba!");
return $response;
});
$app->run($request);
return $app::$response;
});
$socket = new SocketServer('127.0.0.1:8080');
$server->listen($socket);
$loop->run();