Responses¶
Everything a hub sends back is attached to one object. The Response
facade resolves Hubzero\Http\Response,
which extends Symfony's Response and adds output compression and a
chainable header(). The application creates it during boot, the template
fills its content, and the application sends it — so in the ordinary case
you never touch it.
Note
For everything inherited — setStatusCode(), setContent(),
getContent(), headers, setCache() — see the
Symfony HttpFoundation documentation.
This page covers what Hubzero adds and when reaching for it is right.
When you actually need it¶
Four cases, and outside them you should not be touching the response:
- An AJAX task that returns JSON instead of a page.
- A feed or export — CSV, iCalendar, an XML dump — where you set the content type and write the body yourself.
- A system plugin rewriting the finished page on
onAfterRender. - Middleware that must send something before the application would.
Serving a file off disk is not one of them. Use
Hubzero\Content\Server, which handles
disposition, length and byte ranges.
Getting at it¶
or, equivalently, from the container:
Warning
Response is a facade name, and it is also a common model
name — Components\Answers\Models\Response, for one. In a namespaced
file, whichever you use is what the name means, and the two are not
interchangeable. If a file needs both, alias one of them.
Headers¶
header($key, $values, $replace = true) sets a header and returns the
response, so several can be chained:
The most common use by far is switching the content type for an AJAX task, then echoing the payload and ending the request:
App::close() calls exit(). Nothing after it runs, and nothing else gets
appended to the body.
Warning
Leave the App::close() out and the template renders the
whole page after your JSON, so the browser gets valid JSON followed by
HTML and JSON.parse fails on a response that looks right in the network
panel until you scroll. Any stray output before the header call —
a dump(), a blank line after a closing ?> in a file you included —
means the header never goes out at all, because headers cannot follow a
body.
Content¶
setContent($output) replaces the body; it takes a string. getContent()
reads it back. The site template's output is set here at the end of the
render stage, so a system plugin listening on onAfterRender can read the
finished page, alter it, and set it back:
Compression and sending¶
compress($value) marks the response for gzip. The application turns it on
from the gzip global configuration option; when set, send() checks the
browser's Accept-Encoding, confirms zlib is loaded and that headers have
not gone out, and compresses the body on the way past. It is a no-op
otherwise, so nothing breaks on a server without zlib.
send($flush = false) writes the headers and then the body. With $flush
true it also closes the connection — fastcgi_finish_request() where that
exists — so slow work can carry on after the browser has the page. The
application calls this once, at the end of the request; a component calling
it is almost always a mistake.
Serving a file off disk is a separate path with its own headers and byte
ranges. Use Hubzero\Content\Server, not the
response object.
Redirects¶
A redirect is a response too, of a different class.
Hubzero\Http\RedirectResponse
extends Symfony's, and its send() resolves the target first: an
index.php-relative URL is prefixed with Request::base(), line breaks
are stripped, and a URL with no scheme is made absolute against the current
scheme, host and path. That is why a component can redirect to a route
result without worrying whether it came back absolute.
App::redirect() builds one, attaches the request, optionally queues a
message, sends it, and exits:
See redirect for the details, including the message types and what happens to the code after the call.
Status codes and errors¶
Do not set an error status on the response by hand. App::abort($code,
$message) throws the exception the error handler is looking for, and the
error template renders the right page:
| Code | Exception thrown |
|---|---|
| 403 | Hubzero\Error\Exception\NotAuthorizedException |
| 404 | Hubzero\Error\Exception\NotFoundException |
| 405 | Hubzero\Error\Exception\MethodNotAllowedException |
| anything else | Hubzero\Error\Exception\RuntimeException with that code |
Throwing one of those exceptions yourself has the same effect;
App::abort() is the shorter way to say it.
Rewritten and checked against 2.4-main @ 348f0057c2 on 2026-09-10.