Skip to content

Commit

Permalink
Update readme, composer and doc blocks…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteel committed Sep 27, 2016
1 parent 59a67b4 commit 1a326ba
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
72 changes: 71 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,73 @@
# Post/Redirect/Get Middleware for Zend Expressive

WIP
## Intro

This is simple middleware that'll help you implement the PRG pattern in your Zend Expressive app.

Based on the original [Zend Framework PRG controller plugin](https://zendframework.github.io/zend-mvc-plugin-prg/).

Uses `Zend\Session` but performs no configuration of the session itself. I figured this was a sane choice for session management. If you know of a standard session library to typehint against, let me know, but afaik there isn't one…

## Installation

```bash
$ composer require netglue/expressive-prg
```

## Usage

First you'll need to register the middleware with your DI container as an invokable with something like:

```php
// use NetglueExpressive\Middleware\PostRedirectGet;

'dependencies' => [
'invokables' => [
PostRedirectGet::class => PostRedirectGet::class
],
],
```

Add the middleware to your routes whenever you want to perform a PRG something like this:

```php
'routes' => [
'some-form' => [
'name' => 'some-form',
'path' => '/somewhere',
'allowed_methods' => ['GET', 'POST'],
'middleware' => [
PostRedirectGet::class
// Your middleware to post process, render templates etc…
],
],
],
```

Inside your action, the request will have an attribute `prg` set to either false or the posted data _(If any)_, for example:

```php
$post = $request->getAttribute('prg');
if (false === $post) {
// No POST has occurred, probably render the form template
}

// Otherwise, process POST data, validate, store, whatever…
```

The request attribute name is defined as a constant in `PostRedirectGet::KEY`

It is possible for the attribute to be null if the request is neither a GET, nor a POST request and your route allows other methods such as PUT, DELETE etc…

## Tests

```bash
$ cd vendor/netglue/expressive-prg
$ composer install
$ phpunit
```

## About

[Netglue makes web based stuff in Devon, England](https://netglue.uk). We hope this is useful to you and we’d appreciate feedback either way :)

10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
"name": "netglue/expressive-prg",
"description": "Middleware to implement the Post/Redirect/Get pattern in Zend Expressive",
"type": "library",
"keywords": [
"zend",
"expressive",
"netglue",
"prg"
],
"homepage": "https://github.com/netglue/Expressive-PRG",
"require": {
"zendframework/zend-expressive": "^1.0",
"zendframework/zend-session": "^2.7"
Expand All @@ -25,5 +32,8 @@
"psr-4": {
"NetglueExpressiveTest\\": "test/NetglueExpressiveTest"
}
},
"scripts": {
"test": "phpunit"
}
}
15 changes: 12 additions & 3 deletions src/Middleware/PostRedirectGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Zend\Session\Container;
use Zend\Diactoros\Response\RedirectResponse;

/**
* Middleware to implement the PRG Pattern in a Zend Expressive app
*/
class PostRedirectGet
{

Expand All @@ -29,6 +32,7 @@ class PostRedirectGet

/**
* Session Container
*
* @var Container
*/
protected $container;
Expand All @@ -41,8 +45,11 @@ class PostRedirectGet
* @param callable $next
* @return Response
*/
public function __invoke(Request $request, Response $response, callable $next = null)
{
public function __invoke(
Request $request,
Response $response,
callable $next = null
) {
$method = $request->getMethod();
$container = $this->getSessionContainer();

Expand Down Expand Up @@ -79,6 +86,7 @@ public function __invoke(Request $request, Response $response, callable $next =

/**
* Return session container
*
* @return Container
*/
public function getSessionContainer() : Container
Expand All @@ -92,7 +100,8 @@ public function getSessionContainer() : Container

/**
* Set session container
* @param Container $container
*
* @param Container $container
* @return void
*/
public function setSessionContainer(Container $container)
Expand Down

0 comments on commit 1a326ba

Please sign in to comment.