Top Related Projects
PSR HTTP message bridge
Quick Overview
Zend Diactoros is a PHP library that provides implementations of the PSR-7 HTTP message interfaces. It offers a set of classes for handling HTTP requests and responses, making it easier to work with HTTP messages in PHP applications. Diactoros is part of the Zend Framework but can be used independently in any PHP project.
Pros
- Fully compliant with PSR-7 HTTP message interfaces
- Easy to integrate with existing PHP projects
- Well-documented and maintained
- Provides factory methods for creating server-side requests and responses
Cons
- Learning curve for developers new to PSR-7
- May be overkill for simple projects that don't require full HTTP message abstraction
- Requires PHP 7.1 or later, which might be an issue for older projects
Code Examples
Creating a server request:
use Zend\Diactoros\ServerRequestFactory;
$request = ServerRequestFactory::fromGlobals();
Creating a response:
use Zend\Diactoros\Response;
$response = new Response();
$response = $response->withStatus(200)
->withHeader('Content-Type', 'text/html');
$response->getBody()->write('<h1>Hello, World!</h1>');
Handling file uploads:
use Zend\Diactoros\UploadedFile;
$uploadedFile = new UploadedFile(
'/tmp/php1234.tmp',
100,
UPLOAD_ERR_OK,
'example.txt',
'text/plain'
);
Getting Started
To use Zend Diactoros in your project, first install it via Composer:
composer require zendframework/zend-diactoros
Then, you can start using it in your PHP code:
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
$request = ServerRequestFactory::fromGlobals();
$response = new Response();
// Handle the request and modify the response
$response = $response->withStatus(200)
->withHeader('Content-Type', 'application/json');
$response->getBody()->write(json_encode(['message' => 'Hello, World!']));
// Send the response
(new \Zend\Diactoros\Response\SapiEmitter())->emit($response);
This example creates a server request from global variables, creates a response, sets its status and content type, writes a JSON body, and then emits the response using the SAPI emitter.
Competitor Comparisons
PSR HTTP message bridge
Pros of psr-http-message-bridge
- Seamless integration with Symfony components and ecosystem
- Lightweight and focused specifically on bridging PSR-7 messages
- Actively maintained with regular updates and improvements
Cons of psr-http-message-bridge
- Limited functionality compared to zend-diactoros' full PSR-7 implementation
- Requires additional dependencies for complete PSR-7 support
- May have a steeper learning curve for developers not familiar with Symfony
Code Comparison
psr-http-message-bridge:
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
$httpFoundationFactory = new HttpFoundationFactory();
$symfonyRequest = $httpFoundationFactory->createRequest($psrServerRequest);
zend-diactoros:
use Zend\Diactoros\ServerRequestFactory;
$serverRequest = ServerRequestFactory::fromGlobals();
Both libraries aim to provide PSR-7 compatibility, but they approach it differently. psr-http-message-bridge focuses on bridging Symfony's HttpFoundation and PSR-7, while zend-diactoros offers a complete PSR-7 implementation. The choice between them depends on your project's requirements and existing ecosystem.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
zend-diactoros
Repository abandoned 2019-12-31
This repository has moved to laminas/laminas-diactoros.
Diactoros (pronunciation:
/dɪÊktÉrÉs/
): an epithet for Hermes, meaning literally, "the messenger."
This package supercedes and replaces phly/http.
zend-diactoros
is a PHP package containing implementations of the
PSR-7 HTTP message interfaces
and PSR-17 HTTP message factory interfaces.
- File issues at https://github.com/zendframework/zend-diactoros/issues
- Issue patches to https://github.com/zendframework/zend-diactoros/pulls
Documentation
Documentation is available at:
Source files for documentation are in the docs/ tree.
Top Related Projects
PSR HTTP message bridge
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot