Skip to content

Commit

Permalink
add Guzzle PSR7 example to README
Browse files Browse the repository at this point in the history
  • Loading branch information
l0gicgate committed Nov 2, 2018
1 parent dd12345 commit 24c6867
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ $response = $app->run($request, $psr17Factory);
/**
* Once you have obtained the ResponseInterface from App::run()
* You will need to emit the response by using an emitter of your choice
* Slim ships with its own response emitter but
* you could use Zend HttpHandleRunner SapiEmitter for example
* We will use Slim ResponseEmitter for this example
* But you could use Zend HttpHandleRunner SapiEmitter or other
*/
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
Expand Down Expand Up @@ -141,6 +141,42 @@ $responseEmitter = new SapiEmitter();
$responseEmitter->emit($response);
```

## Example Usage With Guzzle PSR-7, Guzzle HTTP Factory

Create an index.php file with the following contents:

```php
<?php
require 'vendor/autoload.php';

use GuzzleHttp\Psr7\ServerRequest;
use Http\Factory\Guzzle\ResponseFactory;
use Slim\ResponseEmitter;

$app = new Slim\App();
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->getBody()->write("Hello, " . $args['name']);
});

$responseFactory = new ResponseFactory();
$request = ServerRequest::fromGlobals();

/**
* The App::run() Method takes 2 parameters
* @param ServerRequestInterface An instantiation of a ServerRequest
* @param ResponseFactoryInterface An instantiation of a ResponseFactory
*/
$response = $app->run($request, $responseFactory);

/**
* Once you have obtained the ResponseInterface from App::run()
* You will need to emit the response by using an emitter of your choice
* We will use Slim ResponseEmitter for this example
*/
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
```

You may quickly test this using the built-in PHP server:
```bash
$ php -S localhost:8000
Expand Down

0 comments on commit 24c6867

Please sign in to comment.