Phico is a work in progress
Phico is currently a work in progress, errors, omissions and changes are to be expected.
Response
A Response should be returned by your app code in response to a Request. The Response is then handled by the ResponseHandler middleware.
response()->json([
"message" => "Are we there yet?"
]);
Status codes
You can set the HTTP status code in the response() helper.
response(422)->json([
"message" => "Please fix these errors",
"errors" => [
"name" => "Please enter your name"
]
]);
Body
Html
Sets an HTML response
response()->html("<h1>Hello World</h1>");
Json
Sets a JSON response
response()->json([
"status" => "success",
"message" => "Saved successfully"
"data" => [
"id" => 456123
]
]);
Text
Sets a text response
response()->text("This is a plain text response, good for a CLI app?");
Inspecting the body
Once the body has been set it can be accesed by the body() method.
$body = $response->body();
Helpers
Download
Sets download headers for a file.
response()->download("/path/to/file", "filename-for-browser.md");
Empty
Sets an empty response with a 204 status code.
response()->empty();
Redirect
Sets the location header for the response
response()->redirect("https://example.com/path/to");
// the default status code is 302 (temporary), override it as needed
response()->redirect("/new/path", 301);
Caching
Cache
Sets the cache-control headers for the response.
// use a timestamp
response()->cache(time() + 36400);
// or a DateTime instance
response()->cache(new DateTime("+1 hour");
Expires
Sets an expiry date for the response.
// use a timestamp
response()->expires(time() + 36400);
// or a DateTime instance
response()->expires(new DateTime("2025-01-01: 00:00:01");
No cache
Sets the no-cache headers for the response
response()->noCache();
Cookies
Returns the list of Cookies attached to the Response.
foreach ($response->cookies() as $cookie) {
// $cookie is an instance of Phico\Http\Response\Cookie
}
Cookie
Sets a Cookie in the response.
response()->cookie("hide-popup", 1);
Headers
Returns the Response Headers instance
foreach (response()->headers() as $header => $content) {
...
}
// the headers object supports has()
if ($response->headers()->has("set-cookie")) {
...
}
Header
Returns the matching header content or null
if (response()->header("content-type") === "text/text") {
...
}