2017 © Pedro Peláez
 

library http

Http library

image

fol/http

Http library

  • Monday, June 8, 2015
  • by oscarotero
  • Repository
  • 1 Watchers
  • 3 Stars
  • 169 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 16 Versions
  • 0 % Grown

The README.md

Fol\Http

Biblioteca Http para PHP 5.5. Aínda que esta dentro do proxecto FOL, pódese usar de xeito independente, (*1)

Build Status Scrutinizer Code Quality, (*2)

Exemplos

Uso básico

use Fol\Http\Request;
use Fol\Http\Response;
use Fol\Http\MiddlewareStack;
use Fol\Http\Middlewares;

//Inicia unha instancia de MiddlewareStack
$stack = new MiddlewareStack();

//Engade alguns middlewares
$stack->push(new Middlewares\Languages());
$stack->push(new Middlewares\Ips());

$stack->push(function ($request, $response, $stack) {
    $response->getBody()->write('Hello world');
    $stack->next();
});

$stack->push(function ($request, $response, $stack) {
    $response->getBody()->setStatus(200);
    $stack->next();
});

//Executaos
$response = $stack->run(new Request('http://domain.com'));

//Envía a resposta ao navegador
$response->send();

Uso con sesións e rutas

As sesións e rutas son como outros "middlewares" que podes engadir ao MiddlewareStack, (*3)

use Fol\Http\Request;
use Fol\Http\Response;
use Fol\Http\MiddlewareStack;

use Fol\Http\Sessions\Native;
use Fol\Http\Routes\Router;

//Inicia o router
$router = new Router();

$router->map([
    'index' => [
        'path' => '/',
        'target' => function ($request, $response) {
            $response->getBody()->write('Esta é a portada');
        }
    ],
    'about' => [
        'path' => '/about',
        'target' => function ($request, $response) {
            $session = $request->attributes->get('SESSION');

            $response->getBody()->write('Ola, ti eres '.$session->get('username'));
        }
    ]
]);

//Inicia o MiddlewareStack
$stack = new MiddlewareStack();

//Engade a middleware da sesión
$stack->push(new Native());

//E tamén a do router
$stack->push($router);

//Executa
$response = $stack->run(new Request('http://domain.com'));

//Envía a resposta
$response->send();

Classes

Request

Xestiona os datos dunha petición http, (*4)

use Fol\Http\Request;

//Crear dende as variables globais ($_SERVER, $_FILES, etc)
$request = Request::createFromGlobals();

//Ou podes crear a túa petición personalizada
$request = new Request('http://blog.com/?page=2', 'GET', ['Accept' => 'text/html']);

//Obxecto para acceder aos datos da url (host, path, query, fragment, etc)
$request->url;

//Accede á "query" (alias de $request->url->query):
$request->query;

//Xestiona cabeceiras
$request->headers

//Xestiona cookies
$request->cookies

//Xestiona os datos parseados do body
$request->data

//Xestiona os arquivos subidos
$request->files

//Devolve o body (streamable)
$body = $response->getBody();

Response

use Fol\Http\Response;

//Crea unha resposta
$response = new Response('Hello world', 200, ['Content-Type' => 'text/html']);

//Xestiona as cabeceiras
$request->headers

//Xestiona as cookies
$request->cookies

//Devolve o body (streamable)
$body = $response->getBody();

MiddlewareStack

Xestiona todos os middlewares en todo o ciclo petición/resposta de http. Para entender mellor o concepto dos middlewares, recomendo ler este artigo, (*5)

Middlewares

Esta biblioteca trae consigo unha serie de middlewares por defecto coas funcionalidades máis comúns:, (*6)

  • BaseUrl: Útil para definir por defecto unha url base, que se usaría tanto para cookies como para o router. Para acceder á url: $request->attributes->get('BASE_URL')
  • BasicAuthentication: Para crear unha autentificación http básica
  • DigestAuthentication: Para crear unha autentificación http de tipo "digest"
  • Formats: Para detectar e normalizar automaticamente o formato da petición (json, txt, html, png, etc). Para acceder ao formato: $request->attributes->get('FORMAT')
  • Ips: Detecta a ip do cliente. Para acceder a ela: $request->attributes->get('IP')
  • Languages: Detecta o idioma preferido polo cliente. Para acceder a el: $request->attributes->get('LANGUAGE')

Sessions

Proporciona unha interface sinxela para traballar con sesións. Para acceder á sesion: $request->attributes->get('SESSION'). Hai dous tipos de sesións:, (*7)

  • Native (usa a implementación nativa de PHP)
  • Session (para traballar con sesións de proba)

Router

Proporciona un sinxelo sistema de enrutamento para MVC. Para acceder á ruta: $request->attributes->get('ROUTE'), (*8)

The Versions

08/06 2015

dev-psr-7

dev-psr-7 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

 

http

23/03 2015

dev-master

9999999-dev https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

 

http

23/03 2015

v6.0.2

6.0.2.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

 

http

23/03 2015

v6.0.1

6.0.1.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

 

http

23/03 2015

v6.0

6.0.0.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

 

http

23/03 2015

6.0.x-dev

6.0.9999999.9999999-dev https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

 

http

17/03 2015

v5.0.1

5.0.1.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

 

http

17/03 2015

v5.0

5.0.0.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

 

http

16/03 2015

v4.0.1

4.0.1.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.0

 

http

02/03 2015

v4.0

4.0.0.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.0

 

http

17/02 2015

v3.0

3.0.0.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.0

 

http

20/01 2015

v2.0.3

2.0.3.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.0

 

http

17/01 2015

v2.0.2

2.0.2.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.0

 

http

17/01 2015

v2.0.1

2.0.1.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.0

 

http

15/01 2015

v2.0.0

2.0.0.0 https://github.com/fol-project/http

Http library

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.0

 

http

24/11 2014

v1.0.0

1.0.0.0 https://github.com/fol-project/fol-http

Http library for FOL framework

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.0