Last active
January 8, 2017 08:05
-
-
Save geerteltink/6846fd63c3b3e886108a62e19ef80746 to your computer and use it in GitHub Desktop.
Expressive PSR-7 Middleware Layers and Action: How to use it properly and pass data with the request.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Middleware; | |
use Psr\Http\Message\ServerRequestInterface; | |
use Psr\Http\Message\ResponseInterface; | |
class BasicMiddleware | |
{ | |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) | |
{ | |
// Step 1: Do something (with the request) first | |
// Step 2: Call the next middleware and wait for the response | |
if ($next) { | |
$response = $next($request, $response); | |
} | |
// Step 3: Do something (with the response) before returning the response | |
// Step 4: Return the response | |
return $response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Middleware; | |
use Psr\Http\Message\ServerRequestInterface; | |
use Psr\Http\Message\ResponseInterface; | |
class PassingDataMiddleware | |
{ | |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) | |
{ | |
// Step 1: Do something first | |
$data = [ | |
'foo' => 'bar', | |
]; | |
// Step 2: Inject data into the request, call the next middleware and wait for the response | |
if ($next) { | |
$response = $next($request->withAttribute(self::class, $data), $response); | |
} | |
// Step 3: Do something (with the response) before returning the response | |
// Step 4: Return the response | |
return $response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Middleware; | |
use Psr\Http\Message\ServerRequestInterface; | |
use Psr\Http\Message\ResponseInterface; | |
class ReceivingDataMiddleware | |
{ | |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) | |
{ | |
// Step 1: Grab the data from the request and use it | |
$data = $request->getAttribute(PassingDataMiddleware::class); | |
// Step 2: Call the next middleware and wait for the response | |
if ($next) { | |
$response = $next($request, $response); | |
} | |
// Step 3: Do something (with the response) before returning the response | |
// Step 4: Return the response | |
return $response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Action; | |
use Psr\Http\Message\ServerRequestInterface; | |
use Psr\Http\Message\ResponseInterface; | |
use App\Middleware\PassingDataMiddleware; | |
class XampleAction | |
{ | |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) | |
{ | |
// Step 1: Grab the data from the request | |
$data = $request->getAttribute(PassingDataMiddleware::class); | |
$id = $request->getAttribute('id'); | |
// Step 2: Do some more stuff | |
// Step 3: Return the response | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment