agungsugiarto / codeigniter4-psr7bridge
CodeIgniter 4 PSR HTTP message bridge
Requires
- php: ^7.2
- codeigniter4/framework: ^4.2
- psr/http-message: ^1.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.17
- nyholm/psr7: ^1.5
- php-http/psr7-integration-tests: ^1.0
Suggests
- : laminas-diactoros is a PHP package containing implementations of the PSR-7 HTTP message interfaces and PSR-17 HTTP message factory interfaces.
- nyholm/psr7: For a super lightweight PSR-7/17 implementation
This package is auto-updated.
Last update: 2024-11-06 12:12:35 UTC
README
The PSR-7 Bridge
The PSR-7 bridge converts codeigniter4-http objects from and to objects implementing HTTP message interfaces defined by the PSR-7.
Table of Contents
Installation
$ composer require agungsugiarto/codeigniter4-psr7bridge
The bridge also needs a PSR-7 and PSR-17 implementation to convert
IncomingRequest
objects to PSR-7 objects. The following command installs the
nyholm/psr7
library, a lightweight and fast PSR-7 implementation.
$ composer require nyholm/psr7
Usage
Converting from IncomingRequest Objects to PSR-7
The bridge provides an interface of a factory called
Fluent\HttpMessageBridge\Interfaces\HttpMessageFactoryInterface
that builds objects implementing PSR-7 interfaces from IncommingRequest
objects.
The following code snippet explains how to convert a CodeIgniter\HTTP\IncomingRequest
to a Nyholm\Psr7\ServerRequest
class implementing the
Psr\Http\Message\ServerRequestInterface
interface:
<?php use CodeIgniter\Config\Services; use Fluent\HttpMessageBridge\PsrHttpFactory; use Nyholm\Psr7\Factory\Psr17Factory; $requestCodeIgniter = Services::request(); $psr17Factory = new Psr17Factory(); $psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory); $psrRequest = $psrHttpFactory->createRequest($requestCodeIgniter);
And now from a CodeIgniter\HTTP\Response
to a
Nyholm\Psr7\Response
class implementing the
Psr\Http\Message\ResponseInterface
interface:
<?php use CodeIgniter\HTTP\Response; use Fluent\HttpMessageBridge\PsrHttpFactory; use Nyholm\Psr7\Factory\Psr17Factory; $responseCodeIgniter = new Response(config('App')); $psr17Factory = new Psr17Factory(); $psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory); $psrResponse = $psrHttpFactory->createResponse($responseCodeIgniter);
License
Released under the MIT License, see LICENSE.