CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Namespace Response

Classes

  • AbstractStreamResponse

    Base class for response types that stream an iterable payload to the client memory-efficiently.

    Concrete subclasses such as {@see \Cake\Http\Response\JsonStreamResponse} produce the wire format (JSON, CSV, XML, …); this class owns the streaming lifecycle that is common to all of them:

    • Wires a {@see \Cake\Http\CallbackStream} into the response body.
    • Emits the streaming-friendly headers (Content-Type with charset and X-Accel-Buffering: no so proxies / nginx do not buffer the response).
    • Provides the {@see self::output()}, {@see self::outputAndFlush()} and {@see self::flushOutputBuffers()} primitives subclasses use to write the wire format, including the threshold-based flush counter driven by the flushEvery option.
    • Provides {@see self::logStreamError()} (gated on cakephp/log being installed) for surfacing encoding failures server-side.
    • Implements the PSR-7 immutability helpers ({@see self::withStreamOptions()} / {@see self::getStreamOptions()}).

    Subclasses must implement {@see self::contentType()} (the MIME type without the charset suffix) and {@see self::streamData()} (the actual write loop). They may override {@see self::normalizeStreamOptions()} to validate format-specific options on top of the base validation.

  • JsonStreamResponse

    A response class for streaming large JSON datasets memory-efficiently.

    Uses true streaming - data is flushed to the client as each item is encoded, keeping memory usage constant regardless of dataset size. Supports both standard JSON arrays and NDJSON (newline-delimited JSON) formats.

    Usage

    // Simple array streaming
    return new JsonStreamResponse($query);
    
    // With root wrapper
    return new JsonStreamResponse($query, ['root' => 'articles']);
    
    // NDJSON format
    return new JsonStreamResponse($query, ['format' => 'ndjson']);

    Options

    • root (string|null, default: null): Wrap data in {"root": [...]}
    • envelope (array, default: []): Static metadata merged with streaming data
    • dataKey (string, default: 'data'): Key for streaming data when envelope is used
    • format (string, default: 'json'): Output format — 'json' or 'ndjson'
    • transform (callable|null, default: null): Transform each item before encoding
    • flags (int, default: DEFAULT_JSON_FLAGS): JSON encode flags
    • flushEvery (int, default: 1): Flush output buffers every N items

    ORM Integration

    For true streaming benefits, use unbuffered queries and avoid result formatters:

    // Good - streams one row at a time
    $query = $this->Articles->find()->disableBufferedResults();
    return new JsonStreamResponse($query);
    
    // Avoid - formatters like map(), combine() buffer results internally
    $query = $this->Articles->find()->map(fn($row) => $row); // Breaks streaming

    Memory Profile

    With true streaming, memory usage stays constant:

    • 10,000 rows @ 1KB each: ~1KB memory (not ~10MB)
    • 100,000 rows @ 1KB each: ~1KB memory (not ~100MB)
    • Time to first byte: after first row (not after all rows)
  • RedirectResponse

    A response redirecting the client to another location.

    The response has an empty body and no Content-Type header.

    Usage

    return new RedirectResponse('/articles', 301, ['X-Reason' => 'moved']);
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs