create() public static method

This function allows an options array which will be used for configuring the session and the handler to be used. The most important key in the configuration array is defaults, which indicates the set of configurations to inherit from, the possible defaults are: - php: just use session as configured in php.ini - cache: Use the CakePHP caching system as an storage for the session, you will need to pass the config key with the name of an already configured Cache engine. - database: Use the CakePHP ORM to persist and manage sessions. By default this requires a table in your database named sessions or a model key in the configuration to indicate which Table object to use. - cake: Use files for storing the sessions, but let CakePHP manage them and decide where to store them. The full list of options follows: - defaults: either 'php', 'database', 'cache' or 'cake' as explained above. - handler: An array containing the handler configuration - ini: A list of php.ini directives to set before the session starts. - timeout: The time in minutes the session should stay active
See also: Cake\Network\Session::__construct()
public static create ( array $sessionConfig = [] ) : Session
$sessionConfig array Session config.
return Session
 /**
  * {@inheritDoc}
  */
 public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null)
 {
     $request = parent::fromGlobals($server, $query, $body, $cookies, $files);
     list($base, $webroot) = static::getBase($request);
     $sessionConfig = (array) Configure::read('Session') + ['defaults' => 'php', 'cookiePath' => $webroot];
     $session = Session::create($sessionConfig);
     $request = $request->withAttribute('base', $base)->withAttribute('webroot', $webroot)->withAttribute('session', $session);
     if ($base) {
         $request = static::updatePath($base, $request);
     }
     return $request;
 }
Beispiel #2
2
 /**
  * Get instance of the session.
  *
  * @return \Cake\Network\Session
  */
 public function getSession()
 {
     if (!empty($this->cake['session'])) {
         return $this->cake['session'];
     }
     if (!empty($this->cake['request'])) {
         $this->cake['session'] = $this->cake['request']->session();
         return $this->cake['session'];
     }
     $config = (array) Configure::read('Session') + ['defaults' => 'php'];
     $this->cake['session'] = Session::create($config);
     return $this->cake['session'];
 }
 /**
  * Wrapper method to create a new request from PHP superglobals.
  *
  * Uses the $_GET, $_POST, $_FILES, $_COOKIE, $_SERVER, $_ENV and php://input data to construct
  * the request.
  *
  * @return \Cake\Network\Request
  */
 public static function createFromGlobals()
 {
     list($base, $webroot) = static::_base();
     $sessionConfig = (array) Configure::read('Session') + ['defaults' => 'php', 'cookiePath' => $webroot];
     $config = ['query' => $_GET, 'post' => $_POST, 'files' => $_FILES, 'cookies' => $_COOKIE, 'environment' => $_SERVER + $_ENV, 'base' => $base, 'webroot' => $webroot, 'session' => Session::create($sessionConfig)];
     $config['url'] = static::_url($config);
     return new static($config);
 }
 /**
  * Creates a request object with the configured options and parameters.
  *
  * @param string|array $url The URL
  * @param string $method The HTTP method
  * @param array|null $data The request data.
  * @return \Cake\Network\Request The built request.
  */
 protected function _buildRequest($url, $method, $data)
 {
     $sessionConfig = (array) Configure::read('Session') + ['defaults' => 'php'];
     $session = Session::create($sessionConfig);
     $session->write($this->_session);
     list($url, $query) = $this->_url($url);
     $props = ['url' => $url, 'post' => $data, 'cookies' => $this->_cookie, 'session' => $session, 'query' => $query];
     $env = [];
     if (isset($this->_request['headers'])) {
         foreach ($this->_request['headers'] as $k => $v) {
             $env['HTTP_' . str_replace('-', '_', strtoupper($k))] = $v;
         }
         unset($this->_request['headers']);
     }
     $env['REQUEST_METHOD'] = $method;
     $props['environment'] = $env;
     $props = Hash::merge($props, $this->_request);
     return new Request($props);
 }
 /**
  * Creates a request object with the configured options and parameters.
  *
  * @param string|array $url The URL
  * @param string $method The HTTP method
  * @param array|null $data The request data.
  * @return array The request context
  */
 protected function _buildRequest($url, $method, $data)
 {
     $sessionConfig = (array) Configure::read('Session') + ['defaults' => 'php'];
     $session = Session::create($sessionConfig);
     $session->write($this->_session);
     list($url, $query) = $this->_url($url);
     $tokenUrl = $url;
     if ($query) {
         $tokenUrl .= '?' . http_build_query($query);
     }
     $props = ['url' => $url, 'post' => $this->_addTokens($tokenUrl, $data), 'cookies' => $this->_cookie, 'session' => $session, 'query' => $query];
     if (is_string($data)) {
         $props['input'] = $data;
     }
     $env = [];
     if (isset($this->_request['headers'])) {
         foreach ($this->_request['headers'] as $k => $v) {
             $name = strtoupper(str_replace('-', '_', $k));
             if (!in_array($name, ['CONTENT_LENGTH', 'CONTENT_TYPE'])) {
                 $name = 'HTTP_' . $name;
             }
             $env[$name] = $v;
         }
         unset($this->_request['headers']);
     }
     $env['REQUEST_METHOD'] = $method;
     $props['environment'] = $env;
     $props = Hash::merge($props, $this->_request);
     return $props;
 }
Beispiel #6
0
 /**
  * test using a handler from a plugin.
  *
  * @return void
  */
 public function testUsingPluginHandler()
 {
     Configure::write('App.namespace', 'TestApp');
     \Cake\Core\Plugin::load('TestPlugin');
     $config = ['defaults' => 'cake', 'handler' => ['engine' => 'TestPlugin.TestPluginSession']];
     $session = Session::create($config);
     $this->assertInstanceOf('TestPlugin\\Network\\Session\\TestPluginSession', $session->engine());
     $this->assertEquals('user', ini_get('session.save_handler'));
 }
 /**
  * Gets current User's ID.
  *
  * @return int User ID, zero if not found
  */
 protected function _getUserId()
 {
     $callable = $this->config('idCallable');
     $id = 0;
     if (is_string($callable)) {
         $session = Session::create();
         $id = $session->read($callable);
     } elseif (is_callable($callable)) {
         $id = $callable();
     }
     return (int) $id;
 }