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 mixed __construct ( Yiisoft\Session\SessionInterface $session )
$session Yiisoft\Session\SessionInterface

                public function __construct(private SessionInterface $session)
{
}

            
add() public method

public void add ( string $key, mixed $value true, boolean $removeAfterAccess true )
$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 mixed get ( string $key )
$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 array getAll ( )

                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 boolean has ( string $key )
$key string

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

            
remove() public method

public void remove ( string $key )
$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 void removeAll ( )

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

            
set() public method

public void set ( string $key, mixed $value true, boolean $removeAfterAccess true )
$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);
}