lithium\action\Request::__construct()

public method

Constructor. Adds config values to the public properties when a new object is created, pulling request data from superglobals if globals is set to true.

Normalizes casing of request headers.

Parameters

  • array $config

    The available configuration options are the following. Further options are inherited from the parent classes.

    • 'base' string: Defaults to null.
    • 'url' string: Defaults to null.
    • 'data' array: Additional data to use when initializing the request. Defaults to [].
    • 'stream' resource: Stream to read from in order to get the message body when method is POST, PUT or PATCH and data is empty. When not provided php://input will be used for reading.
    • 'env' array: Defaults to [].
    • 'globals' boolean: Use global variables for populating the request's environment and data; defaults to true.
    • 'drain' boolean: Enables/disables automatic reading of streams. Defaults to true. Disable when you're dealing with large binary payloads. Note that this will also disable automatic content decoding of stream data.

Returns

void

Source

	public function __construct(array $config = []) {
		$defaults = [
			'base' => null,
			'url' => null,
			'env' => [],
			'data' => [],
			'stream' => null,
			'globals' => true,
			'drain' => true,
			'query' => [],
			'headers' => []
		];
		$config += $defaults;

		if ($config['globals']) {
			if (isset($_SERVER)) {
				$config['env'] += $_SERVER;
			}
			if (isset($_ENV)) {
				$config['env'] += $_ENV;
			}
			if (isset($_GET)) {
				$config['query'] += $_GET;
			}
			if (isset($_POST)) {
				$config['data'] += $_POST;
			}
		}
		$this->_env = $config['env'];

		if (!isset($config['host'])) {
			$config['host'] = $this->env('HTTP_HOST');
		}
		if (!isset($config['protocol'])) {
			$config['protocol'] = $this->env('SERVER_PROTOCOL');
		}
		if ($config['protocol'] && strpos($config['protocol'], '/')) {
			list($scheme, $version) = explode('/', $config['protocol']);

			if (!isset($config['scheme'])) {
				$config['scheme'] = strtolower($scheme) . ($this->env('HTTPS') ? 's' : '');
			}
			if (!isset($config['version'])) {
				$config['version'] = $version;
			}
		}
		$this->_base = $this->_base($config['base']);
		$this->url = $this->_url($config['url']);

		$config['headers'] += [
			'Content-Type' => $this->env('CONTENT_TYPE'),
			'Content-Length' => $this->env('CONTENT_LENGTH')
		];

		foreach ($this->_env as $name => $value) {
			if ($name[0] === 'H' && strpos($name, 'HTTP_') === 0) {
				$name = str_replace('_', ' ', substr($name, 5));
				$name = str_replace(' ', '-', ucwords(strtolower($name)));
				$config['headers'] += [$name => $value];
			}
		}

		parent::__construct($config);
	}