Giter Club home page Giter Club logo

fatfree-psr7-factory's Introduction

HTTP Message plugin for PHP Fat-Free Framework

This is a plugin to enable HTTP Message usage by the framework core.

The goal of this package is to register any PSR-17 compatible http message factory to the frameworks core DI container and use those factories to create and hydrate a Request (or ServerRequest) object with all available data from the hive, so it can be consumed by the route-handling controller or any other package.

Installation

This plugin requires fatfree-core ^4.0, the DI Container enabled and a package of a PSR-7 implementation installed.

composer require f3-factory/fatfree-psr7-factory

Usage

The Http message objects (PSR-7) are created by a related (PSR-17) factory.

At first, we need to register these factories for every message objects. Some PSR-17 factory packages have different classes for each object, some share the same class for all object types.

This example uses the Psr17Factory from our own fatfree-psr7 package, which is very fast and fat-free of course.

To install it run composer require f3-factory/fatfree-psr7-factory OR install any other PSR-7 and PSR-17 package you'd like to use instead.

In your front controller (i.e. index.php):

// create the plugin adapter
$psrAdapter = F3\Http\MessageFactory::instance();
// register the factories:
$psrAdapter->register(
    requestFactory:       Psr17Factory::class,
    responseFactory:      Psr17Factory::class,
    serverRequestFactory: Psr17Factory::class,
    uploadedFileFactory:  Psr17Factory::class,
    uriFactory:           Psr17Factory::class,
    streamFactory:        Psr17Factory::class,
);
// register the concrete Request / Response objects 
$psrAdapter->registerRequest(\F3\Http\Request::class);
$psrAdapter->registerResponse(\F3\Http\Response::class);
$psrAdapter->registerServerRequest(\F3\Http\ServerRequest::class);

Registering the Request, ServerRequest and Response objects will tell the dependency injection container which shortcut it should use to resolve and hydrate the objects accordingly when you're type-hinting them in your route controller.

// TODO: add example of DI usage here

Alternatively you can create a new request object from the currently available $_SERVER and hive data via:

MessageFactory::instance()->makeRequest();
// or 
MessageFactory::instance()->makeServerRequest();

Response and the other classes should be instantiated via the PSR17 factory directly and do not need any special treatment by the framework core.


fatfree-psr7-factory's People

Contributors

ikkez avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.