Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Session Laravel Package

spiral/session

Secure session management for PHP using native handlers, focused on safety and integrity. Part of the Spiral ecosystem, with framework integration available via the Spiral Framework bundle and full docs at spiral.dev. MIT licensed.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer: composer require spiral/session
  2. Register the provider in config/bundles.php:
    Spiral\Session\Binder::class,
    
  3. Configure the session in config/session.php (e.g., driver, lifetime, cookie name). Default driver is files, but supports array, cookie, redis, etc.
  4. First use: Inject \Spiral\Session\SessionInterface into controllers or middleware and use $session->set('key', $value) / $session->get('key').
  5. Flash data: Use $session->setFlash('notice', 'Saved!') → retrieve next request with $session->getFlash('notice').

Implementation Patterns

  • In Controllers: Inject SessionInterface to manage user-specific state (e.g., shopping cart, preferences):
    public function store(SessionInterface $session): Response
    {
        $session->set('cart.items', [...]);
        return $this->response->redirect('/cart');
    }
    
  • Flash for User Feedback: Use in form handling — set flash message on POST, display on redirected GET:
    $session->setFlash('success', 'Profile updated!');
    return $this->redirect->to('profile');
    
  • Middleware Integration: Wrap middleware to auto-apply session start/commit:
    $session->start();
    // ... handle request
    $session->commit(); // persists changes
    
  • Custom Storage: Implement \Spiral\Session\StorageInterface to support databases (e.g.,PDO, Doctrine DBAL), Redis, or custom backends—swap via config driver.

Gotchas and Tips

  • Cookie-only driver: When using cookie driver, remember the 4KB limit per cookie and avoid sensitive data (not encrypted by default).
  • No automatic start: Unlike PHP sessions, session_start() is not called automatically — call $session->start() explicitly (or rely on Binder middleware if used).
  • Flash data expiry: Flash keys are deleted after the next request — not on access. Retrieve them after setting to ensure visibility.
  • Race conditions: Session writes are blocking (lock on start()), avoid long-running operations while holding a session lock. Use $session->close() early if no more writes needed.
  • Testing tip: Use array driver in tests to isolate sessions per test (no file cleanup required).
  • Null safety: Always check existence before access via $session->has('key') or use $session->get('key', $default).
  • Middleware order: Ensure SessionBinder runs before any middleware/controller that relies on session data.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport