public method

Uses results (typically coming from a controller action) to generate content and headers for a Response object.


  • array $options

    An array of options, as follows:

    • 'data': An associative array of variables to be assigned to the template. These are merged on top of any variables set in Controller::set().
    • 'head': If true, only renders the headers of the response, not the body. Defaults to false.
    • 'template': The name of a template, which usually matches the name of the action. By default, this template is looked for in the views directory of the current controller, i.e. given a PostsController object, if template is set to 'view', the template path would be views/posts/view.html.php. Defaults to the name of the action being rendered. The options specified here are merged with the values in the Controller::$_render property. You may refer to it for other options accepted by this method.



Returns the Response object associated with this Controller instance.


	public function render(array $options = []) {
		$media = $this->_classes['media'];
		$class = get_class($this);
		$name = preg_replace('/Controller$/', '', substr($class, strrpos($class, '\\') + 1));
		$key = key($options);

		if (isset($options['data'])) {
		$defaults = [
			'status'     => null,
			'location'   => false,
			'data'       => null,
			'head'       => false,
			'controller' => Inflector::underscore($name),
			'library'    => Libraries::get($class)

		$options += $this->_render + $defaults;

		if ($key && $media::type($key)) {
			$options['type'] = $key;

		$this->_render['hasRendered'] = true;
		$this->response->headers('Location', $options['location']);

		if ($options['head']) {
		$response = $media::render($this->response, $this->_render['data'], $options + [
			'request' => $this->request
		return ($this->response = $response ?: $this->response);