Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thenlabs/http-server

A HTTP Server written in PHP with help of the Symfony components.
https://github.com/thenlabs/http-server

http-server monolog php symfony-httpfoundation symfony-mime symfony-router

Last synced: about 2 months ago
JSON representation

A HTTP Server written in PHP with help of the Symfony components.

Awesome Lists containing this project

README

        

# HttpServer

A HTTP server written in PHP with help of the Symfony Components.

>If you like this project gift us a ⭐.

## Installation.

$ composer require thenlabs/http-server

## Usage.

Create a file with the next example content:

>You should change the content according to your needs.

```php
'127.0.0.1',
'port' => 8080,
'document_root' => __DIR__.'/vendor/thenlabs/http-server/tests/Functional/document_root',
];

$server = new HttpServer($config);
$server->start();

while (true) {
$server->run();
}
```

This file should be executed like that:

$ php run-server.php

Once does it, we can navigate to the URL and we will see the respectively page.

>In our example, we are serving the `index.html` file which is stored within the `tests/document_root` directory.

![](demo.jpg)

### Creating custom routes.

The HttpServer use the [Symfony Routing Component](https://github.com/symfony/routing) for handle the routing, therefore, you can use all the his possibilities.

The next example shown the way to creating a custom route which only can be access by a GET request.

```php
get('/article/{id}', function (Request $request, array $parameters): Response {
return new Response("This is the article {$parameters['id']}");
});

// ...
```

### Using the logs.

Like you can will verify, by default are will shows in the console the result of all server requests.

![](console-logs.png)

The logs are created with help of the popular [Monolog](https://github.com/Seldaek/monolog) library.

The next example showns a way to put the logs in a file.

```php
getLogger()->pushHandler(new StreamHandler('/path/to/file.logs'));
```

## Development.

### Running the tests.

Start the selenium server.

$ java -jar path/to/selenium-server-standalone-x.y.z.jar

Run PHPUnit.

$ ./vendor/bin/pyramidal