Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit993420e

Browse files
Update to new version of the class
1 parentefc82de commit993420e

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

‎components/http_foundation.rst‎

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -590,39 +590,59 @@ Streaming a JSON Response
590590

591591
..versionadded::6.2
592592

593-
The:class:`Symfony\\Component\\HttpFoundation\\StreamedJsonResponse` classallows
594-
an API to return a lot of data as JSON and keep the used resources low by make usage
595-
of Generators.
593+
The:class:`Symfony\\Component\\HttpFoundation\\StreamedJsonResponse` classwas
594+
introduced in Symfony 6.2. It allowsan API to return a lot of data as JSON and keep
595+
the used resources low by make usageof Generators.
596596

597-
It expects a JSON structure with one or multiple replacers identifiers, as example
598-
the `'__articles__'`. As a second argument it requires one or multiple Generators
599-
which items can be converted to a JSON via ``json_encode`` method. The key of the
600-
Generators requires to be the used replacer identifiers::
597+
It expects an array which represents the JSON structure and the list which should be
598+
streamed are represented in the array as ``\Generator``. The response will stream this
599+
JSON with generators in to most efficient way and keep resources as low as possible::
601600

602601
use Symfony\Component\HttpFoundation\StreamedJsonResponse;
603602

603+
function loadArticles(): \Generator { // any method or function returning a Generator
604+
yield ['title' => 'Article 1'];
605+
yield ['title' => 'Article 2'];
606+
yield ['title' => 'Article 3'];
607+
});
608+
604609
$response = new StreamedJsonResponse(
605-
// json structure withreplace identifiers
610+
// json structure withgenerators in which will be streamed as a list
606611
[
607612
'_embedded' => [
608-
'articles' =>'__articles__',
613+
'articles' =>loadArticles(), // any \Generator can be used which will be streamed as list of data
609614
],
610615
],
611-
// array of generators with replace identifier used as key
612-
[
613-
'__articles__' => (function (): \Generator { // any method or function returning a Generator
614-
yield ['title' => 'Article 1'];
615-
yield ['title' => 'Article 2'];
616-
yield ['title' => 'Article 3'];
617-
})(),
618-
]
619616
);
620617

621618
..tip::
622619

623620
If loading data via doctrine the ``toIterable`` method of ``Doctrine`` can be
624621
used to keep also the resources low and fetch only one row one by one.
625-
See the `Doctrine Batch processing`_ documentation for more.
622+
See the `Doctrine Batch processing`_ documentation for more::
623+
624+
public function __invoke(): Response
625+
{
626+
return new StreamedJsonResponse(
627+
[
628+
'_embedded' => [
629+
'articles' => $this->loadArticles(),
630+
],
631+
],
632+
);
633+
}
634+
635+
public function loadArticles(): \Generator
636+
{
637+
$queryBuilder = $entityManager->createQueryBuilder();
638+
$queryBuilder->from(Article::class, 'article');
639+
$queryBuilder->select('article.id')
640+
->addSelect('article.title')
641+
->addSelect('article.description');
642+
643+
return $queryBuilder->getQuery()->toIterable();
644+
}
645+
626646

627647
Serving Files
628648
~~~~~~~~~~~~~

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp