0 follower

Final Class Yiisoft\Session\Flash\Flash

InheritanceYiisoft\Session\Flash\Flash
ImplementsYiisoft\Session\Flash\FlashInterface

Session-based implementation of flash messages, a special type of data, that is available only in the current request and the next request. After that, it will be deleted automatically. Flash messages are particularly useful for displaying confirmation messages.

Constants

Hide inherited constants

Constant Value Description Defined By
COUNTERS '__counters' Yiisoft\Session\Flash\Flash
FLASH_PARAM '__flash' Yiisoft\Session\Flash\Flash

Method Details

Hide inherited methods

__construct() public method

public __construct( Yiisoft\Session\SessionInterface $session ): mixed
$session Yiisoft\Session\SessionInterface

                public function __construct(private SessionInterface $session)
{
}

            
add() public method

public add( string $key, mixed $value true, boolean $removeAfterAccess true ): void
$key string
$value mixed
$removeAfterAccess boolean

                public function add(string $key, $value = true, bool $removeAfterAccess = true): void
{
    $flashes = $this->fetch();
    /** @psalm-suppress MixedArrayAssignment */
    $flashes[self::COUNTERS][$key] = $removeAfterAccess ? -1 : 0;
    if (empty($flashes[$key])) {
        $flashes[$key] = [$value];
    } elseif (is_array($flashes[$key])) {
        $flashes[$key][] = $value;
    } else {
        $flashes[$key] = [$flashes[$key], $value];
    }
    $this->save($flashes);
}

            
get() public method

public get( string $key ): mixed
$key string

                public function get(string $key)
{
    $flashes = $this->fetch();
    if (!isset($flashes[$key], $flashes[self::COUNTERS][$key])) {
        return null;
    }
    if ($flashes[self::COUNTERS][$key] < 0) {
        // Mark for deletion in the next request.
        $flashes[self::COUNTERS][$key] = 1;
        $this->save($flashes);
    }
    return $flashes[$key];
}

            
getAll() public method

public getAll( ): array

                public function getAll(): array
{
    $flashes = $this->fetch();
    $list = [];
    foreach ($flashes as $key => $value) {
        if ($key === self::COUNTERS) {
            continue;
        }
        $list[$key] = $value;
        if ($flashes[self::COUNTERS][$key] < 0) {
            // Mark for deletion in the next request.
            $flashes[self::COUNTERS][$key] = 1;
        }
    }
    $this->save($flashes);
    return $list;
}

            
has() public method

public has( string $key ): boolean
$key string

                public function has(string $key): bool
{
    $flashes = $this->fetch();
    return isset($flashes[$key], $flashes[self::COUNTERS][$key]);
}

            
remove() public method

public remove( string $key ): void
$key string

                public function remove(string $key): void
{
    $flashes = $this->fetch();
    unset($flashes[self::COUNTERS][$key], $flashes[$key]);
    $this->save($flashes);
}

            
removeAll() public method

public removeAll( ): void

                public function removeAll(): void
{
    $this->save([self::COUNTERS => []]);
}

            
set() public method

public set( string $key, mixed $value true, boolean $removeAfterAccess true ): void
$key string
$value mixed
$removeAfterAccess boolean

                public function set(string $key, $value = true, bool $removeAfterAccess = true): void
{
    $flashes = $this->fetch();
    /** @psalm-suppress MixedArrayAssignment */
    $flashes[self::COUNTERS][$key] = $removeAfterAccess ? -1 : 0;
    $flashes[$key] = $value;
    $this->save($flashes);
}