PHP code example of easyconn / reactphp-slim

1. Go to this page and download the library: Download easyconn/reactphp-slim library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

easyconn / reactphp-slim example snippets




use \mbarquin\reactSlim;

// We keep a new Slim app instance.
$app = new \Slim\App();

// We add a closure to attend defined request routes
$app->any('/hello/{name}', function (
        \Slim\Http\Request $request,
        \Slim\Http\Response $response) {

        $name = $request->getAttribute('name');
        $response->getBody()->write("Hello, $name");

        return $response;
    });

$server = new \mbarquin\reactSlim\Server();

$server->withPort(1337)->run($app);



use mbarquin\reactSlim;

// We keep a new Slim app instance.
$app = new \Slim\App();

// We add a closure to listen defined request routes
$app->get('/hello/{name}', function (
        \mbarquin\reactSlim\Request $request,
        \mbarquin\reactSlim\Response $response) {

        $name = $request->getAttribute('name');
        $response->getBody()->write("Hello, $name");

        return $response;
    });

// We create a closure to be attached to server request event.
$serverCallback = function (
       \React\Http\Request $request,
       \React\Http\Response $response) use ($app){

    $slRequest  = \mbarquin\reactSlim\Request::createFromReactRequest($request);
    $slResponse = new \mbarquin\reactSlim\Response();

    $app->process($slRequest, $slResponse);

    $slResponse->setReactResponse($response, true);
};

// We make the setup of the ReactPHP
$loop   = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http   = new React\Http\Server($socket, $loop);

// Ligamos la closure al evento request.
$http->on('request', $serverCallback);

echo "Server running at http://127.0.0.1:1337\n";

$socket->listen(1337);
$loop->run();