Skip to main content
  1. Posts/

Sending Strings as Files with CakePHP server response

·77 words·1 min·
PHP Back End CakePHP php

From time to time you need to send string over response as file e.g. confiiuration files or calendar invites as follows bellow:

public function sendIcs()
{
  $icsString = $this->Calendars->generateIcs();
  $response = $this->response;

  // Inject string content into response body
  $response = $response->withStringBody($icsString);

  $response = $response->withType('ics');

  // Optionally force file download
  $response = $response->withDownload('filename_for_download.ics');

  // Return response object to prevent controller from trying to render
  // a view.
  return $response;
}

Example above is from official CakePHP book.