Skip to content

Commit

Permalink
Update to work with Slim Framework 4
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanjhv committed Aug 25, 2019
1 parent 18728e6 commit 35936cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.**

Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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"

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions src/Slim/Middleware/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 35936cd

Please sign in to comment.