PHP code example of meekframework / http

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

    

meekframework / http example snippets




use FastRoute\simpleDispatcher;
use FastRoute\RouteCollector;
use FastRoute\Dispatcher;
use Meek\Http\ClientError;
use Meek\Http\ClientError\MethodNotAllowed;
use Meek\Http\ClientError\NotFound;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response\TextResponse;
use Zend\Diactoros\Response\SapiEmitter;

$dispatcher = simpleDispatcher(function(RouteCollector $r) {
    $r->addRoute('GET', '/users', 'get_all_users_handler');
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');
    $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler');
});

$request = ServerRequestFactory::fromGlobals();
$routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getRequestTarget());

try {
    switch ($routeInfo[0]) {
        case Dispatcher::NOT_FOUND:
            throw new NotFound();
            break;

        case Dispatcher::METHOD_NOT_ALLOWED:
            $allowedMethods = $routeInfo[1];
            throw new MethodNotAllowed($allowedMethods);
            break;

        case Dispatcher::FOUND:
            $response = call_user_func_array($routeInfo[1], $routeInfo[2]);
            break;
    }
} catch (ClientError $httpClientError) {
    $response = new TextResponse((string) $httpClientError);    // response body...
    $response = $httpClientError->prepare($response);   // add headers, code, etc...
}

(new SapiEmitter())->emit($response);