PHPackages                             neat/http-server - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. neat/http-server

ActiveLibrary[HTTP &amp; Networking](/categories/http)

neat/http-server
================

Neat HTTP server components

0.3.2(7mo ago)02.0k[7 issues](https://github.com/neat-php/http-server/issues)[1 PRs](https://github.com/neat-php/http-server/pulls)MITPHPPHP ^7.4 || ^8.0

Since Dec 19Pushed 7mo ago3 watchersCompare

[ Source](https://github.com/neat-php/http-server)[ Packagist](https://packagist.org/packages/neat/http-server)[ RSS](/packages/neat-http-server/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (6)Versions (18)Used By (0)

Neat HTTP Server components
===========================

[](#neat-http-server-components)

[![Stable Version](https://camo.githubusercontent.com/02bced7cf2020ed05fcca98795b5b5338d75d5401c089a7213fcdab3bc8512c0/68747470733a2f2f706f7365722e707567782e6f72672f6e6561742f687474702d7365727665722f76657273696f6e)](https://packagist.org/packages/neat/http-server)[![Build Status](https://camo.githubusercontent.com/851befd5aa7c52d47e6aa5e7bd87d07aaddf83bb1c739268da897b83d1313814/68747470733a2f2f7472617669732d63692e6f72672f6e6561742d7068702f687474702d7365727665722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/neat-php/http-server)

Neat HTTP server components provide a clean and expressive API for your application to receive HTTP requests and send HTTP responses.

Requirements
------------

[](#requirements)

To use Neat HTTP Server components you will need

- PHP 7.0 or newer
- a [PSR-7 HTTP message implementation](https://packagist.org/providers/psr/http-message-implementation)
- a [PSR-17 HTTP factory implementation](https://packagist.org/providers/psr/http-factory-implementation)

Getting started
---------------

[](#getting-started)

To install this package, simply issue [composer](https://getcomposer.org) on the command line:

```
composer require neat/http-server

```

Server
------

[](#server)

```
// Create a PSR-17 factory
$factory = new Example\Factory();

// Then create the server using this factory (three interfaces are required)
$server = new Neat\Http\Server\Server($factory, $factory, $factory);

// Write a handler to handle incoming requests
$handler = new Neat\Http\Server\Handler\CallableHandler(function (Neat\Http\Server\Request $request) {
    // return new Neat\Http\Response(...);
});
```

Then use the server to receive the request, handle the request and send the response back:

```
/** @var Neat\Http\Server\Server $server */
/** @var Neat\Http\Server\Handler $handler */

// Receive the request
$request = $server->receive();

// Handle the request
$response = $handler->handle($request);

// Send the response
$server->send($response);
```

Handlers
--------

[](#handlers)

Handlers can be written from scratch using the Handler interface or created using one of the provided adapters:

```
// Write a handler from scratch
class Handler implements Neat\Http\Server\Handler
{
    /** @noinspection PhpInconsistentReturnPointsInspection */
    public function handle(Neat\Http\Server\Request $request): Neat\Http\Response
    {
        // return new Neat\Http\Response(...);
    }
}

// Alternatively write a handler using a closure
$handler = new Neat\Http\Server\Handler\CallableHandler(function (Neat\Http\Server\Request $request) {
    // return new Neat\Http\Response(...);
});

// Or use an existing PSR-15 RequestHandlerInterface implementation
/** @var Psr\Http\Server\RequestHandlerInterface $psr */
$handler = new Neat\Http\Server\Handler\PsrHandler($psr);
```

Middleware
----------

[](#middleware)

To intercept incoming requests, outgoing responses and possibly exceptions, you can create a Middleware that adds an extra layer of control over your handler and the messages going in and out.

```
// Write a middleware from scratch
class Middleware implements Neat\Http\Server\Middleware
{
    public function process(Neat\Http\Server\Request $request, Neat\Http\Server\Handler $handler): Neat\Http\Response
    {
        return $handler->handle($request);
    }
}

// Or using a closure
$handler = new Neat\Http\Server\Middleware\CallableMiddleware(
function (Neat\Http\Server\Request $request, Neat\Http\Server\Handler $handler) {
    return $handler->handle($request);
});
```

Dispatcher
----------

[](#dispatcher)

To use your middleware when handling the request, you can use the Dispatcher.

```
// Assuming we have a handler readily available, we can create a Dispatcher
// with a stack of one or more Middleware instances
/** @var Neat\Http\Server\Handler $handler */
$dispatcher = new Neat\Http\Server\Dispatcher(
    $handler,
    new Neat\Http\Server\Middleware\CallableMiddleware(function () { /* ... */ }),
    new Neat\Http\Server\Middleware\CallableMiddleware(function () { /* ... */ })
);

// Then using the request we can ask the dispatcher to handle the request and
// return the response from the handler through the middleware.
/** @var Neat\Http\Server\Request $request */
$response = $dispatcher->handle($request);
```

Output
------

[](#output)

Creating responses from your controllers is real easy

```
// First create the output helper using a PSR-17 factory and a templating renderer
$factory = new Example\Factory();
$output  = new Neat\Http\Server\Output($factory, $factory, function (string $template, array $data) {
    // Use any templating engine you like and return the rendered result as a string
});

// Then create a simple text response (it will have the proper Content-Length and Content-Type header set)
$response = $output->text('Hello world!');

// Or an html response (with the text/html Content-Type header)
$response = $output->html('Hi!');

// Us a custom body and add headers afterwards
$response = $output->body('{key:"value"}')->withContentType('application/json');

// Or just let the output create a JSON response directly
$response = $output->json(['key' => 'value']);

// There's even XML support
$response = $output->xml('Long live XML');

// Rendering a template view is just as easy using the output helper
$response = $output->view('template', ['message' => 'Hello world!']);

// Download a file
$response = $output->download(fopen('path/to/really/large/file.bin', 'r+'));

// Display it inline
$response = $output->display('path/to/file.pdf');

// Other types of responses
$response = $output->response(404, 'Page not found');
```

Redirect
--------

[](#redirect)

Redirecting a client to another URL is easy using the redirect output helper.

```
/** @var Neat\Http\Server\Input $input */
/** @var Neat\Http\Server\Output $output */
/** @var Neat\Http\Server\Request $request */

// Redirect to a url
$response = $output->redirect()->to('/go/there/instead');

// Redirect permanently
$response = $output->redirect()->permanent()->to('/go/there/instead');

// Redirect and resubmit
$response = $output->redirect()->resubmit()->to('/submit/there/instead');

// Redirect back to the referring url
$response = $output->redirect()->back($request);

// Refresh
$response = $output->redirect()->refresh($request);

// Retry input
$response = $output->redirect()->retry($input);
```

In your handler you can use the output helper to convert any return value other than a Neat\\Http\\Response into one.

```
$factory = new Example\Factory();
$output  = new Neat\Http\Server\Output($factory, $factory);

// By default any value that isn't a Neat\Http\Response will be converted to a JSON response
$response = $output->resolve(['What now?' => 'My controller just returned this lousy array.']);

// You can learn the output to handle any type using a Response factory
$output->register('string', function (string $string) use ($output) {
    return $output->html($string);
});
$response = $output->resolve('HELP my controller just returned a string!');

// Null could be your way of returning a 204 No content response
$output->register('null', function () use ($output) {
    return $output->response(204);
});

// If you want objects with a __toString method to convert differently
$output->register('object', function ($object) use ($output) {
    if (method_exists($object, '__toString')) {
        return (string) $object;
    }

    return $output->json($object);
});

// You can even target specific classes or interfaces
$output->register(Psr\Http\Message\StreamInterface::class, function (Psr\Http\Message\StreamInterface $stream) use ($output) {
    return $output->response()->withBody($stream);
});
```

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance62

Regular maintenance activity

Popularity18

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 78.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~143 days

Recently: every ~266 days

Total

16

Last Release

236d ago

PHP version history (3 changes)0.1.0PHP &gt;=7.0

0.2.0PHP ^7.2 || ^8.0

0.3.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7a15e12278809e0c3e4b7f18a87fd181de619dbc2bbd781676f892f19122d5ec?d=identicon)[annavanbiemen](/maintainers/annavanbiemen)

---

Top Contributors

[![baukevdw](https://avatars.githubusercontent.com/u/6784391?v=4)](https://github.com/baukevdw "baukevdw (30 commits)")[![annavanbiemen](https://avatars.githubusercontent.com/u/1430899?v=4)](https://github.com/annavanbiemen "annavanbiemen (8 commits)")

---

Tags

httpmiddlewarepsr-17serverhandlerneat

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/neat-http-server/health.svg)

```
[![Health](https://phpackages.com/badges/neat-http-server/health.svg)](https://phpackages.com/packages/neat-http-server)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

7.9k1.1B3.7k](/packages/guzzlehttp-psr7)[cakephp/cakephp

The CakePHP framework

8.8k19.1M1.7k](/packages/cakephp-cakephp)[laminas/laminas-stratigility

PSR-7 middleware foundation for building and dispatching middleware pipelines

577.0M95](/packages/laminas-laminas-stratigility)[mezzio/mezzio

PSR-15 Middleware Microframework

3903.8M120](/packages/mezzio-mezzio)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28545.4k3](/packages/mezzio-mezzio-authentication-oauth2)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28146.3k](/packages/phpro-http-tools)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
