Coder Social home page Coder Social logo

slim-session's Introduction

slim-session

Simple middleware for Slim Framework 3, 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 2, please check out the slim-2 branch in this repository.

Installation

Include this line in your composer.json:

"bryanjhv/slim-session": "~3.0"

Usage

$app = new \Slim\App;
$app->add(new \Slim\Middleware\Session([
  'name' => 'dummy_session',
  'autorefresh' => true,
  'lifetime' => '1 hour'
]));

Supported options

  • lifetime: How long a session can be. Defaults to 20 minutes (yes, you can pass anything that can be parsed by strtotime).
  • path, domain, secure, httponly: Options for the session cookie.
  • name: Name for the session cookie. Defaults to slim_session (instead of PHP's PHPSESSID).
  • autorefresh: Set this to true if you want the session to be refresh each time a user activity is made.

Session helper

This package also ships with a Helper class which you need to register in the container or instance as an object if you want to use it:

$container = $app->getContainer();

$container['session'] = function ($c) {
  return new \SlimSession\Helper;
};

This will provide you $app->session, so you can simply do:

$app->get('/', function () {
  $session = new \SlimSession\Helper; // or $this->session if registered

  // Get a variable
  $key = $session->get('key', 'default');
  $st = $session->st;

  // Set a variable
  $session->my_key = 'my_value';
  $app->session->set('a', 'var');

  // Remove variable
  $session->delete('a_var');

  // Destroy session
  $session::destroy();

  // Get current session id
  $id = $this->session::id();
});

TODO

Tests (still, still)!

License

MIT

slim-session's People

Contributors

bryanjhv avatar farkie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.