PHP code example of fol / http

1. Go to this page and download the library: Download fol/http library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fol / http example snippets


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();

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();

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();

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();