PHP Classes

ReactPHP HTTP Server Response: Generate HTTP server responses using middleware

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 47 All time: 10,756 This week: 673Up
Version License PHP version Categories
reaponse 1.0Shareware5HTTP, PHP 5
Description 

Author

This package can generate HTTP server responses using middleware.

It provides an example of how to use middleware classes that can generate responses to requests handled by ReactPHP HTTP server.

The package provides an example of a request counter.

Picture of Ahmad Mustapha
Name: Ahmad Mustapha <contact>
Classes: 24 packages by
Country: Nigeria Nigeria
Age: ???
All time rank: 226511 in Nigeria Nigeria
Week rank: 178 Up7 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 9x

Example

<?php

use React\EventLoop\Factory;
use
React\Socket\Server;
use
Reaponse\Http\Middleware;

require
'vendor/autoload.php';

$loop = Factory::create();
$uri = '0.0.0.0:9200';

$myServer = new \Test\ServerHandler();
$myCounter = new \Test\CounterHandler();

$httpServer = new \React\Http\Server($loop, new Middleware($myCounter, $myServer));
$socketServer = new Server($uri, $loop);

$httpServer->listen($socketServer);
$httpServer->on('error', function (Throwable $throwable){
    echo
$throwable;
});

echo
"Server started at http://{$uri}\n";
$loop->run();


Details

ReactPHP Response Helper

This library provides beautiful syntax for ReactPHP HTTP component, this library provides syntax very similar to that of NodeJS.

Installation

You will need Composer to install this library.

composer require ahmard/reactphp-response

Usage

Registering middleware

  • Test\Counter
namespace Test;

use Reaponse\Http\HandlerInterface;
use Reaponse\Http\ResponseInterface;

class CountHandler implements HandlerInterface
{
    protected static int $counts = 0;

    public function handle(ResponseInterface $response): void
    {
        self::$counts++;
        $response->write('Count: ' . self::$counts);
        $response->handler()->next();
    }
}

  • Test\Server
namespace Test;

use Reaponse\Http\HandlerInterface;
use Reaponse\Http\ResponseInterface;

class ServerHandler implements HandlerInterface
{
    public function handle(ResponseInterface $response): void
    {
        $response->html(', Time: ' . date('H:i:s'));
        $response->end('.');
    }
}

  • server.php
use React\EventLoop\Factory;
use React\Socket\Server;
use Reaponse\Http\Middleware;
use Test\CounterHandler;
use Test\ServerHandler;

require 'vendor/autoload.php';

$loop = Factory::create();
$uri = '0.0.0.0:9200';

$myServer = new ServerHandler();
$myCounter = new CounterHandler();

$httpServer = new \React\Http\Server($loop, new Middleware($myCounter, $myServer));
$socketServer = new Server($uri, $loop);

$httpServer->listen($socketServer);
$httpServer->on('error', function (Throwable $throwable){
    echo $throwable;
});

echo "Server started at http://{$uri}\n";
$loop->run();

Start the server

php server.php

Request object

use Reaponse\Http\HandlerInterface;
use Reaponse\Http\ResponseInterface;


class TestHandler implements HandlerInterface
{
    public function handle(ResponseInterface $response): void
    {
        //psr-7 compliant object
        $request = $response->request();
        
        $response->html("Method: {$request->getMethod()}<br/>");
        
        $response->end('Bye!');
    }
}

Listens to response events

use Reaponse\Http\HandlerInterface;
use Reaponse\Http\Response;
use Reaponse\Http\ResponseInterface;


class TestHandler implements HandlerInterface
{
    public function handle(ResponseInterface $response): void
    {
        //listens to write event
        $response->on(Response::ON_WRITE, function (){
            echo "Writing...\n";
        });
        //Listens to headers event
        $response->on(Response::ON_HEADERS, function (){
            echo "Headers...\n";
        });
        //Listens to next handler event
        $response->on(Response::ON_NEXT_HANDLER, function (){
            echo "Next handler...\n";
        });
        //Listens to response sending event
        $response->on(Response::ON_BEFORE_SEND, function (){
            echo "Sending...\n";
        });
        
        $response->end('Hello World');
    }
}

  • All handlers must implement HandlerInterface
  • A handler is a middleware, handler is just a fancy name given to it.

Example

Licence

Reaponse is MIT licenced.


  Files folder image Files (13)  
File Role Description
Files folder imageexample (3 files)
Files folder imagesrc (1 file, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (13)  /  example  
File Role Description
  Plain text file CounterHandler.php Class Class source
  Accessible without login Plain text file server.php Example Example script
  Plain text file ServerHandler.php Class Class source

  Files folder image Files (13)  /  src  
File Role Description
Files folder imageHttp (5 files)
  Plain text file ObjectStorage.php Class Class source

  Files folder image Files (13)  /  src  /  Http  
File Role Description
  Plain text file Handler.php Class Class source
  Plain text file HandlerInterface.php Class Class source
  Plain text file Middleware.php Class Class source
  Plain text file Response.php Class Class source
  Plain text file ResponseInterface.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:47
This week:0
All time:10,756
This week:673Up