snicco/session-bundle

v1.9.0 2023-09-20 12:36 UTC

README

codecov Psalm Type-Coverage Psalm level PhpMetrics - Static Analysis PHP-Versions

This WordPress bundle integrates snicco/session in applications based on snicco/kernel.

The snicco/http-routing-bundle is required for this bundle.

Installation

composer install snicco/session-bundle

Configuration

See config/session.php for the available configuration options.

If this file does not exist in your configuration directory the default configuration will be copied the first time the kernel is booted in dev mode.

Add the SessionBundle to your bundles.php config file.

<?php
// /path/to/configuration/bundles.php

use Snicco\Bundle\Session\SessionBundle;

return [
    
    'bundles' => [
        Snicco\Component\Kernel\ValueObject\Environment::ALL => [
           SessionBundle::class
        ]   
    ]   
];

Usage

This bundle contains several middleware that manage the session lifecycle.

Its recommended configure the session middleware in the middleware config of the HttpRoutingBundle like so:

//path/to/config/middleware.php
use Snicco\Bundle\HttpRouting\Option\MiddlewareOption;
use Snicco\Bundle\Session\Middleware\AllowMutableSessionForReadVerbs;
use Snicco\Bundle\Session\Middleware\SaveResponseAttributes;
use Snicco\Bundle\Session\Middleware\SessionNoCache;
use Snicco\Bundle\Session\Middleware\ShareSessionWithViews;
use Snicco\Bundle\Session\Middleware\StatefulRequest;

return [

    MiddlewareOption::GROUPS => [
        'stateful' => [
            StatefulRequest::class,
            ShareSessionWithViews::class,
            SaveResponseAttributes::class,
//            SessionNoCache::class, optional
        ]   
    ],
    MiddlewareOption::ALIASES => [
        'session-allow-write' => AllowMutableSessionForReadVerbs::class,
        'session-no-cache' => SessionNoCache::class,
    ]
];

The session can be accessed on the PSR-7 request:

// inside a controller or middleware
use Snicco\Component\Session\ImmutableSession;
use Snicco\Component\Session\MutableSession;

$request->getAttribute(ImmutableSession::class);

// Only for unsafe request methods or if allowed explicitly for read requests.
$request->getAttribute(MutableSession::class);

Contributing

This repository is a read-only split of the development repo of the Snicco project.

This is how you can contribute.

Reporting issues and sending pull requests

Please report issues in the Snicco monorepo.

Security

If you discover a security vulnerability within BetterWPCache, please follow our disclosure procedure.