lemric / batch-request
Send a single HTTP request that contains multiple (batch) Symfony Request calls. Once all operations are complete, a consolidated response is passed back to you and the HTTP connection is closed.
Requires
- php: >=8.2
Requires (Dev)
- dg/bypass-finals: ^1.11
- friendsofphp/php-cs-fixer: ^v3.95
- illuminate/http: >=v12.0.0
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^13.3
- symfony/config: >= 6.4
- symfony/dependency-injection: >= 6.4
- symfony/http-client: >=5.1
- symfony/http-kernel: >=5.1
- symfony/phpunit-bridge: >=5.1
- symfony/polyfill-php83: ^v1.41
- symfony/rate-limiter: >=7.0
- symfony/routing: >=7.0
- symfony/var-dumper: >= 6.4
Suggests
- illuminate/support: Required for Laravel integration (^10.0|^11.0)
- symfony/dependency-injection: Required to use the bundled Symfony BatchRequestBundle (^6.4|^7.0|^8.0).
- symfony/rate-limiter: A "rate limiter" controls how frequently some event (e.g. an HTTP request or a login attempt) is allowed to happen.
- symfony/twig-bundle: Required to render the Batch Request profiler panel template (^6.4|^7.0|^8.0).
- symfony/web-profiler-bundle: Required to display the Batch Request panel in the Symfony Profiler (^6.4|^7.0|^8.0).
Provides
None
Conflicts
None
Replaces
None
README
Batch HTTP API calls into a single request. Compatible with Symfony 6.4+ / 7 / 8 and Laravel 10+.
Send one JSON envelope containing multiple operations. Independent read-only operations may run in parallel (PHP Fibers); write operations stay serialised. When every operation finishes, a consolidated JSON response is returned and the HTTP connection is closed.
Requirements
| Requirement | Version |
|---|---|
| PHP | ≥ 8.2 |
| Symfony* | ^6.4 | ^7 | ^8 |
| Laravel* | ^10 | ^11 | ^12 |
* Framework packages are optional — install only the bridge you need. See Installation.
Installation
composer require lemric/batch-request
Quick Start
Symfony
// config/bundles.php return [ // ... Lemric\BatchRequest\Bridge\Symfony\BatchRequestBundle::class => ['all' => true], ];
use Lemric\BatchRequest\Bridge\Symfony\SymfonyBatchRequestFacade; use Symfony\Component\HttpFoundation\{Request, Response}; use Symfony\Component\Routing\Attribute\Route; #[Route('/batch', name: 'batch', methods: ['POST'])] public function batch(Request $request, SymfonyBatchRequestFacade $batch): Response { return $batch->handle($request); }
Note: The legacy
Lemric\BatchRequest\BatchRequestclass is deprecated and will be removed in 3.2. PreferSymfonyBatchRequestFacade.
Laravel
// bootstrap/providers.php (Laravel 11+) or config/app.php Lemric\BatchRequest\Bridge\Laravel\LaravelServiceProvider::class,
use Illuminate\Http\{JsonResponse, Request}; use Lemric\BatchRequest\Bridge\Laravel\LaravelBatchRequestFacade; public function __invoke(Request $request, LaravelBatchRequestFacade $batch): JsonResponse { return $batch->handle($request); }
Example request
curl -X POST https://example.com/batch \ -H 'Content-Type: application/json' \ -d '[ {"method":"GET","relative_url":"/api/users/1"}, {"method":"GET","relative_url":"/api/users/2"}, {"method":"POST","relative_url":"/api/posts","body":{"title":"Hello"}} ]'
Example response
[
{"code": 200, "body": {"id": 1, "name": "Alice"}},
{"code": 200, "body": {"id": 2, "name": "Bob"}},
{"code": 201, "body": {"id": 42, "title": "Hello"}}
]
The order of responses always matches the order of operations.
Documentation
| Topic | Description |
|---|---|
| Installation | Composer setup, optional dependencies |
| Usage | Request / response protocol |
| Responses | Content types, binary payloads, headers |
| Errors | Sub-request failures & top-level errors |
| Security | Hardening model & threat mitigations |
| Architecture | Handler, Fibers, validators |
| Symfony integration | Bundle, configuration, profiler, rate limiting |
| Laravel integration | Service provider, configuration, controller |
Full table of contents: docs/index.md.
Features
- Framework bridges for Symfony (
BatchRequestBundle) and Laravel - Fiber-based concurrency for contiguous read-only (
GET/HEAD) groups, with bounded concurrency - OWASP-oriented validation (path traversal, CRLF, SSRF, recursive batches, header injection)
- RFC 7807 problem documents for failed sub-responses and top-level errors
- Mixed media types in one batch (JSON, HTML, XML, binary/base64,
BinaryFileResponse,StreamedResponse) - Symfony Web Profiler panel (dev)
- Optional rate limiting via
symfony/rate-limiter
Security
If you discover a security vulnerability, please follow the process described in SECURITY.md. Do not open a public issue.
Contributing
- Run the test suite:
composer test - Static analysis:
composer analyse - Bug reports and pull requests are welcome on GitHub.
License
This package is released under the MIT license.
