Cake\Network\Session::consume PHP Method

consume() public method

Reads and deletes a variable from session.
public consume ( string $name ) : mixed
$name string The key to read and remove (or a path as sent to Hash.extract).
return mixed The value of the session variable, null if session not available, session not started, or provided name not found in the session.
    public function consume($name)
    {
        if (empty($name)) {
            return null;
        }
        $value = $this->read($name);
        if ($value !== null) {
            $this->_overwrite($_SESSION, Hash::remove($_SESSION, $name));
        }
        return $value;
    }

Usage Example

Example #1
0
 /**
  * Test consuming session data.
  *
  * @return void
  */
 public function testConsume()
 {
     $session = new Session();
     $session->write('Some.string', 'value');
     $session->write('Some.array', ['key1' => 'value1', 'key2' => 'value2']);
     $this->assertEquals('value', $session->read('Some.string'));
     $value = $session->consume('Some.string');
     $this->assertEquals('value', $value);
     $this->assertFalse($session->check('Some.string'));
     $value = $session->consume('');
     $this->assertNull($value);
     $value = $session->consume(null);
     $this->assertNull($value);
     $value = $session->consume('Some.array');
     $expected = ['key1' => 'value1', 'key2' => 'value2'];
     $this->assertEquals($expected, $value);
     $this->assertFalse($session->check('Some.array'));
 }
All Usage Examples Of Cake\Network\Session::consume