PHP Classes

ReactPHP HTTP Server Response: Generate HTTP server responses using middleware

Recommend this page to a friend!
  Info   View files Example   View files View files (13)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2021-11-19 (15 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 35 All time: 10,300 This week: 319Up
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: 17 packages by
Country: Nigeria Nigeria
Innovation award
Innovation award
Nominee: 6x

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  
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  /  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  /  src  
File Role Description
Files folder imageHttp (5 files)
  Plain text file ObjectStorage.php Class Class source

  Files folder image Files  /  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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:35
This week:0
All time:10,300
This week:319Up
For more information send a message to info at phpclasses dot org.