From 35936cdf14091cefbba0bf7fd862688533c5ce1a Mon Sep 17 00:00:00 2001 From: Bryan Horna Date: Sun, 25 Aug 2019 04:07:08 -0500 Subject: [PATCH] Update to work with Slim Framework 4 --- README.md | 24 ++++++++++++++---------- composer.json | 4 ++-- src/Slim/Middleware/Session.php | 10 +++++----- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c6387e4..fcdf2b9 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ # slim-session     [![Donate][paybtn]][paylnk] -Simple middleware for [Slim Framework 3][slim], that allows managing PHP +Simple middleware for [Slim Framework 4][slim], that allows managing PHP built-in sessions and includes a `Helper` class to help you with the `$_SESSION` superglobal. +**For the middleware version for Slim Framework 3, please check out the `slim-3` +branch in this repository.** + **For the middleware version for Slim Framework 2, please check out the `slim-2` branch in this repository.** @@ -26,7 +29,7 @@ composer require bryanjhv/slim-session:~3.0 ## Usage ```php -$app = new \Slim\App; +$app = \Slim\Factory\AppFactory::create(); $app->add(new \Slim\Middleware\Session([ 'name' => 'dummy_session', 'autorefresh' => true, @@ -63,19 +66,20 @@ $app->add(new \Slim\Middleware\Session([ A `Helper` class is available, which you can register globally or instantiate: ```php -$container = $app->getContainer(); +$container = new \DI\Container; // Register globally to app -$container['session'] = function ($c) { +$container->set('session', function () { return new \SlimSession\Helper; -}; +}); +\Slim\Factory\AppFactory::setContainer($container); ``` -That will provide `$app->session`, so you can do: +That will provide `$app->get('session')`, so you can do: ```php $app->get('/', function ($req, $res) { - // or $this->session if registered + // or $this->get('session') if registered $session = new \SlimSession\Helper; // Check if variable exists @@ -89,12 +93,12 @@ $app->get('/', function ($req, $res) { $my_value = $session['my_key']; // Set variable value - $app->session->set('my_key', 'my_value'); + $app->get('session')->set('my_key', 'my_value'); $session->my_key = 'my_value'; $session['my_key'] = 'my_value'; // Merge value recursively - $app->session->merge('my_key', ['first' => 'value']); + $app->get('session')->merge('my_key', ['first' => 'value']); $session->merge('my_key', ['second' => ['a' => 'A']]); $letter_a = $session['my_key']['second']['a']; // "A" @@ -130,7 +134,7 @@ $app->get('/', function ($req, $res) { MIT -[slim]: https://www.slimframework.com/docs/v3/ +[slim]: https://www.slimframework.com/docs/v4/ [sesscfg]: http://php.net/manual/en/session.configuration.php [contributors]: https://github.com/bryanjhv/slim-session/graphs/contributors diff --git a/composer.json b/composer.json index 34faa9a..bdffd29 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "bryanjhv/slim-session", - "description": "Session middleware and helper for Slim framework 3.", + "description": "Session middleware and helper for Slim framework 4.", "type": "library", "keywords": [ "slim", @@ -22,7 +22,7 @@ "source": "https://github.com/bryanjhv/slim-session.git" }, "require": { - "slim/slim": "^3.0" + "slim/slim": "^4.0" }, "require-dev": { "phpunit/phpunit": "^7.0" diff --git a/src/Slim/Middleware/Session.php b/src/Slim/Middleware/Session.php index 36973ec..2a2ae35 100644 --- a/src/Slim/Middleware/Session.php +++ b/src/Slim/Middleware/Session.php @@ -4,6 +4,7 @@ use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; +use Psr\Http\Server\RequestHandlerInterface as RequestHandler; /** * Session middleware @@ -65,17 +66,16 @@ public function __construct($settings = []) /** * Called when middleware needs to be executed. * - * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request - * @param \Psr\Http\Message\ResponseInterface $response PSR7 response - * @param callable $next Next middleware + * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request + * @param \Psr\Http\Server\RequestHandlerInterface $handler PSR7 handler * * @return \Psr\Http\Message\ResponseInterface */ - public function __invoke(Request $request, Response $response, callable $next) + public function __invoke(Request $request, RequestHandler $handler): Response { $this->startSession(); - return $next($request, $response); + return $handler->handle($request); } /**