PHP code example of patrick-barreto / http

1. Go to this page and download the library: Download patrick-barreto/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/ */

    

patrick-barreto / http example snippets


use Http\Http;

    Http::post(string $endpoint, Closure $controllerCallBack, array $middlewares = []);
    

    Http::get(string $endpoint, Closure $controllerCallBack, array $middlewares = [])
    

    Http::put(string $endpoint, Closure $controllerCallBack, array $middlewares = [])
    

    Http::patch(string $endpoint, Closure $controllerCallBack, array $middlewares = [])
    

    Http::delete(string $endpoint, Closure $controllerCallBack, array $middlewares = [])
    



use Http\Middleware\MiddlewareInterface;

class Middleware implements MiddlewareInterface {
    public function handler($request, $callback){
        //Put your code here, change the $request instance..
        return $callback($request);
    }
}

Http::middleware(string $name, string $namespace, bool $defaultForAllRoutes = false)

Http::response($response = ['success'=>true],  int $statusCode = 200, array $headers = ["Content-Type" => "application/json"]) ;

public static function getMethod()

public static function getHeaders()
   
public static function getRoute()
            
public static function getQueryStrings()
    
public static function getPathParams()
   
public static function getAllStaticData()
            
public static function getBody()

    Http::run()
    

Http::CORS(
            array $accessControlAllowOrigin = ['*'], 
            array $accessControlAllowMethods = ['POST', 'GET', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
            array $accessControlAllowHeaders = ['Content-Type'],
            bool $accessControlAllowCredentials = false,
            array $accessControlExposeHeaders = ['Content-Type'],
            int $accessControlMaxAge = 86400 
        );


namespace Api\Middlewares;

use Http\Middleware\MiddlewareInterface;

class Authorization implements MiddlewareInterface {
    public function handler($request, $callback){
        $request->auth = $this->validadeToken($request);
        return $callback($request);
    }

    private function validadeToken($request){
        //Validade with request headers and return true or false
    }
}


namespace Api\Middlewares;

use Http\Middleware\MiddlewareInterface;

class AccessTokenValidade implements MiddlewareInterface {
    public function handler($request, $callback){
        //Validade your accessToken
        return $callback($request);
    }
}


use Http\Http;

//Middlewares default
Http::middleware('AccessTokenValidade', 'Api\Middlewares\AccessTokenValidade', true);

//Middlewares to routes use.
Http::middleware('Auth', 'Api\Middlewares\Authorization');



use Http\Http;

Http::post('/createAccount', function($request) {
    $return = new YourController($request);
    Http::response());

Http::post('/login', function($request) {
    $return = new YourController($request);
    Http::response($return, 
                    200, 
                    [
                        ["Content-Type" => "application/json"],
                        ['Authorization' => $return['JWT']],
                    ]);
                });

Http::get('/users', function($request) {
    $return = new YourController($request);
    Http::response($return)
}, 
['Auth']);

    Http::loadRoutesFromPath(string $dirRoutesName);

    

    use Http\Http;

    .php";

    Http\Http::CORS();

    Http::loadRoutesFromPath('./Routes/')
    
    //if you need to put anything more in this system part, put here.

    Http::run();