Skip to content

Use JsonResponse/BinaryFileResponse instead of Response #16752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,10 @@ Returning JSON Response
To return JSON from a controller, use the ``json()`` helper method. This returns a
``JsonResponse`` object that encodes the data automatically::

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
// ...

public function index(): Response
public function index(): JsonResponse
{
// returns '{"username":"jane.doe"}' and sets the proper Content-Type header
return $this->json(['username' => 'jane.doe']);
Expand All @@ -623,10 +623,10 @@ Streaming File Responses
You can use the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::file`
helper to serve a file from inside a controller::

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
// ...

public function download(): Response
public function download(): BinaryFileResponse
{
// send the file contents and force the browser to download it
return $this->file('/path/to/some_file.pdf');
Expand All @@ -638,7 +638,7 @@ The ``file()`` helper provides some arguments to configure its behavior::
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
// ...

public function download(): Response
public function download(): BinaryFileResponse
{
// load the file from the filesystem
$file = new File('/path/to/some_file.pdf');
Expand Down