HTTP Response
- Introduction
- Sending a response
- Modifying the response
- Sending JSON
- Sending a response from views
- Redirection
Introductionβ
All routes and controllers must return a response to be sent back to the user. Bow provides several different ways to return responses. The most basic response is returning a string from a route or a controller. Bow will automatically convert the string into a complete HTTP response.
Sending a responseβ
A stringβ
An HTTP response can be a string.
$app->get('/', function ()
{
return "Hello, world";
});
A Collection and an array or an Objectβ
An HTTP response can be an instance of Bow\Support\Collection.
$app->get('/array', function ()
{
return [10, 2, 12, 'name' => 'Dakia'];
});
$app->get('/collection', function ()
{
return collect([10, 2, 12]);
});
$app->get('/object', function ()
{
return (object) ['name' => 'Dakia'];
});
A BARRY modelβ
The HTTP response can be a Model or a Collection of models; Bow converts it directly to JSON.
use App\Models\User;
$app->get('/', function ()
{
$model = User::where('id', 1)->first();
return $model;
});
Modifying the responseβ
Modifying the error codeβ
It is very important to add error codes to your HTTP response if you are developing a RESTFUL API. The status method allows you to do this.
use App\Models\User;
$app->get('/', function ()
{
$response = response();
return $response->status(400);
});
For more information about HTTP codes, check out this link.
If, in your API, the request was processed successfully and you have no information to return, it is better to return a 204 than a 200 π.
Adding an HTTP headerβ
You will often need to add other HTTP headers to your HTTP response. The addHeader and withHeaders methods allow you to do this.
$response->addHeader('Content-Type', 'application/json');
You can add several headers at the same time:
$response->withHeaders([
'Content-Type' => 'application/json',
'X-Proto-Value' => 1
]);
Sending JSONβ
In REST applications (REST/RESTFUL APIs) information is generally returned in JSON. To send JSON information to the client, you can use the json helper or the json method on the Bow\Http\Response instance.
Prototype of the json methodβ
$response->json($data, $code, $headers);
| Parameter | Type | Description |
|---|---|---|
| $data | Array, stdClass, Iterable, JsonSerializable | The data to serialize |
| $code | Int | The HTTP status code |
| $headers | Array | The headers to add to the response |
JSON sending exampleβ
Example:
$data = ['message' => 'Hello, World'];
return $response->json($data, $code = 200, [
'X-Proto-Value' => 1 // Just a test
]);
With the HELPER:
$data = ['message' => 'Hello, World'];
return json($data, 200, [
'X-Proto-Value' => 1 // Just a test
]);
Here, Bow directly adds the Content-Type HTTP header set to application/json, so there is no need to add it manually in the $headers.
Sending a response from viewsβ
You can often use an instance of Bow\Http\Response to render a view via the render method.
Prototype of the render methodβ
$response->render($name, $data, $code, $headers);
| Parameter | Type | Description | |----------|------| | $name | String - The name of a view to render | | $data | Array - The data to send to the view | | $code | Int - The HTTP status code | | $headers | Array - The headers to add to the response |
View sending exampleβ
Example:
$users = User::where('id', '!=', 2)->get();
return $response->render('users', ['users' => $users], 200, [
'X-Proto-Value' => 1 // Just a test
]);
Downloading a fileβ
You will often need to set up systems for downloading zip files or images, or you may build applications where you have to authenticate before downloading files, like Dropbox. Bow offers a simple API via the Bow\Http\Response class to download a file with the download method.
Prototype of the download methodβ
$response->download($file, $filename = null, $headers, $disposition = 'attachment');
| Parameter | Type | Description |
|---|---|---|
| $file | String | The absolute path of the file |
| $filename | String | The new file name; defaults to null |
| $headers | Array | The headers to add to the response |
| $disposition | String | Indicates whether the content should be displayed inline in the browser, i.e. as a web page, or as an attachment that is downloaded and saved locally. |
More information about the Content-Disposition header.
Download exampleβ
Example:
$file = '/path/to/file.png';
return $response->download($file, 'image.png');
Redirectionβ
You will certainly need to perform redirections to other resources, and this is in fact an HTTP response. This is done with the to and back methods (the latter, as its name suggests, allows you to go back) of the Bow\Http\Redirect class.
Redirection with toβ
Redirection with to allows you to send the user to another page.
return redirect()->to($path);
Prototype of toβ
| Parameter | Type | Description |
|---|---|---|
| $path | String | The redirection URL |
return redirect()->to('/users');
Redirection with backβ
Redirection with back allows you to send the user to the previous page.
return redirect()->back($status_code);
Prototype of backβ
| Parameter | Type |
|---|---|
| $status_code | Int - The HTTP code of the response; defaults to 302 |
return redirect()->back();
Redirection with informationβ
You can also perform a redirection along with the information sent by the user, using the withInput and withFlash methods, like this:
return redirect()->back()->withInput();
// Or
return redirect()->to("/home")->withInput();
With a flash message:
return redirect()->back()->withFlash('success', 'Papac');
Note that you can pass an array of values to withInput, which will be a collection of information to send to the user.
Is something missing?
If you run into problems with the documentation or have suggestions to improve the documentation or the project in general, please open an issue for us, or send a tweet mentioning the Twitter account @bowframework or directly on github.