Cake\Network\Session::consume PHP 메소드

consume() 공개 메소드

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).
리턴 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

예제 #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