1. Go to this page and download the library: Download velt/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/ */
velt / http example snippets
use Velt\Http\Integration\HttpServiceProvider;
use Velt\Kernel\Application;
$app = new Application(dirname(__DIR__));
$app->registerProvider(HttpServiceProvider::class);
$app->boot();
use App\Users\Controllers\UserController;
use Velt\Http\JsonResponse;
use Velt\Http\Router;
return static function (Router $router): void {
$router->get('/', static fn (): string => 'Velt');
$router->get('/api/users/{id}', [UserController::class, 'show']);
$router->post('/api/users', static fn (): JsonResponse =>
JsonResponse::json(['created' => true], 201)
);
};
use Velt\Http\Response;
use Velt\Http\JsonResponse;
$html = new Response('<h1>Hello</h1>', 200, [
'Content-Type' => 'text/html; charset=utf-8',
]);
$json = JsonResponse::json([
'data' => ['id' => 10],
], 200);
final class UserController
{
public function show(Request $request, string $id): JsonResponse
{
return JsonResponse::json([
'data' => ['id' => $id],
]);
}
}
use Velt\Http\MiddlewareInterface;
use Velt\Http\Request;
use Velt\Http\ResponseInterface;
final class RequestIdMiddleware implements MiddlewareInterface
{
public function handle(Request $request, callable $next): ResponseInterface
{
$response = $next($request);
return $response->header('X-Request-Id', bin2hex(random_bytes(8)));
}
}
$route->middleware(RequestIdMiddleware::class);
$dispatcher = new Dispatcher(
$router,
allowedOrigins: ['https://app.example']
);
$token = $csrf->token();
echo $csrf->field();
if (!$csrf->validateRequest($request)) {
throw new HttpException(419, 'CSRF token mismatch.');
}
use Velt\Http\Dispatcher;
use Velt\Http\Request;
$kernel = her'];
$response = $dispatcher->dispatch(Request::capture());
$response->send();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.