Final Class Yiisoft\Session\Flash\Flash
| Inheritance | Yiisoft\Session\Flash\Flash |
|---|---|
| Implements | Yiisoft\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.
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| COUNTERS | '__counters' | Yiisoft\Session\Flash\Flash | |
| FLASH_PARAM | '__flash' | Yiisoft\Session\Flash\Flash |
Method Details
| public mixed __construct ( Yiisoft\Session\SessionInterface $session ) | ||
| $session | Yiisoft\Session\SessionInterface | |
public function __construct(private SessionInterface $session)
{
}
| 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);
}
| 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];
}
| 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;
}
| public boolean has ( string $key ) | ||
| $key | string | |
public function has(string $key): bool
{
$flashes = $this->fetch();
return isset($flashes[$key], $flashes[self::COUNTERS][$key]);
}
| 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);
}
| public void removeAll ( ) |
public function removeAll(): void
{
$this->save([self::COUNTERS => []]);
}
| 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);
}
Signup or Login in order to comment.