Session
equip/session is an optional library for using sessions in Equip applications.
The benefit of using session objects instead of a global variable is primarily in testing, where the session object can be mocked and operations verified. Using session objects also makes it much easier to switch to a distributed session storage as your application scales.
Configuration
To use the native session implementation the configuration must be enabled in the application bootstrap:
Equip\Application::build()
->setConfiguration([
// ...
Equip\Configuration\SessionConfiguration::class,
])
// ...
Basic Example
namespace Acme\Domain;
use Equip\SessionInterface;
use Equip\Adr\DomainInterface;
class WidgetDomain implements DomainInterface
{
/**
* @var SessionInterface
*/
private $session;
public function __construct(
SessionInterface $session
) {
$this->session = $session;
}
public function __invoke(array $input)
{
// Do things with $this->session, etc...
}
}
Usage
The session object can be modified using methods or with array operations:
// Set a value using object methods:
$session->set('foo', 'bar');
// Or using array operations:
$session['foo'] = 'bar';
// Get a value:
$foo = $session->get('foo');
// Or:
$foo = $session['foo'];
// Check for a value:
if ($session->has('foo')) { /* ... */ }
// Or:
if (isset($session['foo'])) { /* ... */ }
// Delete a value
$session->del('foo');
// Or:
unset($session['foo']);
The entire session can also be cleared at any time:
$session->clear();