PHP code example of milpa / http

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

    

milpa / http example snippets


use Milpa\Http\HttpMethod;
use Milpa\Http\Routing\Route;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class ShowUser
{
    #[Route('/users/{id}', HttpMethod::GET, name: 'users.show')]
    public function __invoke(ServerRequestInterface $request): ResponseInterface
    {
        // ...
    }
}

use Milpa\Http\Routing\Route;

return [
    ...Route::behind($gate,
        Route::get('/workspace/hub', [HubController::class, 'connect'], 'desktop.hub'),
        Route::post('/workspace/settings', [MutationController::class, 'saveSettings'], 'desktop.settings.save'),
    ),
    Route::get('/assets/{file}', [AssetsController::class, 'component'], 'desktop.assets.component'),
];

use Milpa\Http\Routing\MatchStatus;
use Milpa\Http\Routing\RouteResult;
use Milpa\Http\Routing\RouterInterface;

/** @var RouterInterface $router */
$result = $router->match($request);

$response = match ($result->status) {
    MatchStatus::MATCHED => $handlerResolver
        ->resolve($result->route->handler)
        ->handle($request->withAttribute(RouteResult::ATTRIBUTE, $result)),
    MatchStatus::NOT_FOUND         => $responseFactory->createResponse(404),
    MatchStatus::METHOD_NOT_ALLOWED => $responseFactory->createResponse(405),
};