Skip to content

Repository files navigation

Effectra Session

The Session class represents a session and provides methods for session management.

Table of Contents

Installation

You can install the Session class via Composer by running the following command:

composer require effectra/session

Usage

To use the Session class, you need to include it in your PHP file:

require_once'vendor/autoload.php';
useEffectra\Session\Session;
// Create a new instance of the Session class$session = newSession();
// Start the session$session->start();
// Use session methods to manage session data// ...// Save and close the session$session->save();

Methods

The Session class provides the following methods:

  • start(): Starts the session.
  • save(): Saves and closes the session.
  • isActive(): Checks if the session is active.
  • get(string $key, mixed $default = null): Retrieves the value for the given key from the session.
  • has(string $key): bool: Checks if the given key exists in the session.
  • regenerate(): bool: Regenerates the session ID.
  • put(string $key, mixed $value): Sets a value in the session for the given key.
  • forget(string $key): Removes the value for the given key from the session.
  • flash(string $key, array $messages): Sets a flash message in the session for the given key.
  • getFlash(string $key): array: Retrieves the flash message for the given key from the session.
  • all(): array: Gets all of the session data.
  • pull(string $key, mixed $default = null): mixed: Gets the value of a given key and then forgets it.
  • clear(): void: Removes all of the items from the session.
  • destroy(): void: Destroys the session entirely.
  • getId(): string: Gets the current session ID.
  • setId(string $id): void: Sets the session ID.

Dependency Injection (DI) Container Usage

The Session class provides default values for its constructor parameters, making it fully compatible with auto-wiring Dependency Injection Containers. To support various frameworks out-of-the-box, this package includes specific integrations:

Laravel

The package includes a Service Provider (Effectra\Session\Providers\SessionServiceProvider) that is automatically discovered by Laravel. You can resolve the SessionInterface or Session directly from the container:

useEffectra\Session\Contracts\SessionInterface;
// In a controller or servicepublicfunction__construct(SessionInterface$session) {
$this->session = $session;
}

Slim / PSR-11 Containers

For frameworks like Slim or generic PSR-11 containers, you can use the included SessionFactory:

useEffectra\Session\Contracts\SessionInterface;
useEffectra\Session\Factory\SessionFactory;
// In your definitions.php or dependencies setupreturn [
SessionInterface::class => DI\factory(SessionFactory::class),
];

If your container supports auto-wiring (like PHP-DI), it can also instantiate Effectra\Session\Session directly thanks to the default constructor values.

Example

Here's an example of how to use the Session class:

// Create a new instance of the Session class$session = newSession();
// Start the session$session->start();
// Set a value in the session$session->put('username', 'john_doe');
// Get a value from the session$username = $session->get('username');
// Check if a key exists in the sessionif ($session->has('username')) {
// Do something
}
// Pull a value (get and remove)$pulledValue = $session->pull('username');
// Regenerate the session ID$session->regenerate();
// Set a flash message in the session$session->flash('success', ['Logged in successfully!']);
// Retrieve and display the flash message$successMessage = $session->getFlash('success');
echo$successMessage[0];
// Save and close the session$session->save();

About

Effectra/Session class represents a session and provides methods for session management.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages