{"id":21023519,"url":"https://github.com/jahidulpabelislam/http","last_synced_at":"2026-01-03T16:13:43.065Z","repository":{"id":65283225,"uuid":"542826236","full_name":"jahidulpabelislam/http","owner":"jahidulpabelislam","description":"Basic classes around HTTP for PHP.","archived":false,"fork":false,"pushed_at":"2024-09-06T23:12:53.000Z","size":76,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"1.x","last_synced_at":"2025-01-20T13:41:16.204Z","etag":null,"topics":["http","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jahidulpabelislam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-28T23:03:53.000Z","updated_at":"2024-09-06T23:12:56.000Z","dependencies_parsed_at":"2023-11-12T14:43:48.552Z","dependency_job_id":"42da8af0-045b-4398-bde4-b7f7bb39061d","html_url":"https://github.com/jahidulpabelislam/http","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fhttp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fhttp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fhttp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jahidulpabelislam%2Fhttp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jahidulpabelislam","download_url":"https://codeload.github.com/jahidulpabelislam/http/tar.gz/refs/heads/1.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243456563,"owners_count":20293905,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http","php"],"created_at":"2024-11-19T11:18:29.210Z","updated_at":"2026-01-03T16:13:43.060Z","avatar_url":"https://github.com/jahidulpabelislam.png","language":"PHP","readme":"# HTTP\n\n[![CodeFactor](https://www.codefactor.io/repository/github/jahidulpabelislam/http/badge)](https://www.codefactor.io/repository/github/jahidulpabelislam/http)\n[![Latest Stable Version](https://poser.pugx.org/jpi/http/v/stable)](https://packagist.org/packages/jpi/http)\n[![Total Downloads](https://poser.pugx.org/jpi/http/downloads)](https://packagist.org/packages/jpi/http)\n[![Latest Unstable Version](https://poser.pugx.org/jpi/http/v/unstable)](https://packagist.org/packages/jpi/http)\n[![License](https://poser.pugx.org/jpi/http/license)](https://packagist.org/packages/jpi/http)\n![GitHub last commit (branch)](https://img.shields.io/github/last-commit/jahidulpabelislam/http/1.x.svg?label=last%20activity)\n\nA simple \u0026 lightweight HTTP library providing components to build web applications and APIs in PHP.\n\nThis library has been kept very simple, following the KISS principle.\n\n## Features\n\n- **Routing**: Routes with parameters, controller classes or closures as handlers, named routes for URL generation\n- **Middleware Support**\n- **Request Handling**: Access query parameters, input, uploaded files, headers, and cookies\n- **Response Building**: Fluent interface to create text or JSON responses\n\n## Dependencies\n\n- PHP 8.0+\n- Composer\n- [jpi/utils](https://packagist.org/packages/jpi/utils) v1\n\n## Installation\n\nUse [Composer](https://getcomposer.org/)\n\n```bash\n$ composer require jpi/http \n```\n\n## Usage\n\nThis library consists of several main components that work together to handle HTTP requests and responses:\n\n- **App**: The main application container that manages routing and middleware\n- **Router**: Handles route registration and matching\n- **Request**: Represents an HTTP request with access to parameters, headers, body, and files\n- **Response**: Represents an HTTP response with status codes, headers, and body content\n- **Route**: Defines a single route pattern with its handler\n- **Middleware**: Chain of processors that can modify requests/responses\n\n### Basic Setup\n\nTo create a basic HTTP application, you'll need to instantiate the main components:\n\n```php\n$request = \\JPI\\HTTP\\Request::fromGlobals();\n\n$router = new \\JPI\\HTTP\\Router(\n    $request,\n    function (\\JPI\\HTTP\\Request $request): \\JPI\\HTTP\\Response {\n        return new \\JPI\\HTTP\\Response(404, \"Not Found\");\n    },\n    function (\\JPI\\HTTP\\Request $request): \\JPI\\HTTP\\Response {\n        return new \\JPI\\HTTP\\Response(405, \"Method Not Allowed\");\n    }\n);\n\n$app = new \\JPI\\HTTP\\App($router);\n```\n\n### Defining Routes\n\nRoutes are defined using the `addRoute` method (on `App` \u0026 `Router`), which accepts a path pattern, HTTP method, callback, and optional name:\n\n```php\n// Simple GET route\n$app-\u003eaddRoute(\"/\", \"GET\", function (\\JPI\\HTTP\\Request $request): \\JPI\\HTTP\\Response {\n    return new \\JPI\\HTTP\\Response(200, \"Hello, World!\");\n});\n\n// POST route\n$app-\u003eaddRoute(\"/posts/\", \"POST\", function (\\JPI\\HTTP\\Request $request): \\JPI\\HTTP\\Response {\n    // Process the data...\n    return new \\JPI\\HTTP\\Response(201, \"Post created\");\n});\n```\n\n### Using Controllers\n\nInstead of closures, you can use classes for better organisation, format is `{class}::{method}` (method must be public):\n\n```php\nfinal class PostController {\n\n    use \\JPI\\HTTP\\RequestAwareTrait;\n\n    public function index(): \\JPI\\HTTP\\Response {\n        // Access request via $this-\u003egetRequest()\n        return new \\JPI\\HTTP\\Response(200, \"Posts\");\n    }\n}\n\n$app-\u003eaddRoute(\"/posts/\", \"GET\", \"PostController::index\");\n```\n\nNote: closures get the request as the first argument, where as Controllers have access to the request via the `RequestAwareTrait`.\n\n### Route Parameters\n\nRoute parameters are defined using curly braces (e.g. `{param}`) and then are passed as arguments to your route handler:\n\n```php\n$app-\u003eaddRoute(\"/posts/{category}/{id}/\", \"GET\", function (\\JPI\\HTTP\\Request $request, string $category, string $id): \\JPI\\HTTP\\Response {\n    return new \\JPI\\HTTP\\Response(200, \"Post\");\n});\n```\n\n### Named Routes\n\nYou can assign names to routes, which allows you to generate URLs for them later:\n\n```php\n$app-\u003eaddRoute(\"/posts/{slug}/\", \"GET\", \"PostController::show\", \"post.show\");\n```\n\nTo generate URLs for named routes:\n\n```php\n$path = $router-\u003egetPathForRoute(\"post.show\", [\"slug\" =\u003e \"my-post\"]);\n// Result: /posts/my-post/\n\n$url = $router-\u003egetURLForRoute(\"post.show\", [\"slug\" =\u003e \"my-post\"]);\n// Result: \\JPI\\Utils\\URL object with full URL\n```\n\n### Request Object\n\nThe Request object provides access to all incoming request data, see [API Reference](docs/API.md) for full details.\n\n### Response Object\n\nThe Response object allows you to build HTTP responses:\n\n```php\n$response = new \\JPI\\HTTP\\Response(200, \"Hello, World!\");\n\n$response = \\JPI\\HTTP\\Response::json(200, [\"message\" =\u003e \"Success\"]);\n\n$response = (new \\JPI\\HTTP\\Response())\n    -\u003ewithStatus(200)\n    -\u003ewithHeader(\"Content-Type\", \"text/html\")\n    -\u003ewithBody(\"\u003ch1\u003eHello\u003c/h1\u003e\")\n    -\u003ewithCacheHeaders([\n        \"Cache-Control\" =\u003e \"public, max-age=3600\",\n        \"ETag\" =\u003e true, // Automatically generated from body\n    ])\n;\n```\n\n### Middleware\n\nMiddleware allows you to process requests before they reach your route handlers. Your class will need to implement `\\JPI\\HTTP\\RequestMiddlewareInterface`. You can add a middle using `addMiddleware` on the app or pass an array to the App constructor:\n\n```php\nclass AuthMiddleware implements \\JPI\\HTTP\\RequestMiddlewareInterface {\n\n    use \\JPI\\HTTP\\RequestAwareTrait;\n\n    public function run(\\JPI\\HTTP\\RequestHandlerInterface $next): \\JPI\\HTTP\\Response {\n        $token = $this-\u003egetRequest()-\u003egetHeaderString(\"Authorization\");\n\n        if (!$token) {\n            return new \\JPI\\HTTP\\Response(401, \"Unauthorized\");\n        }\n\n        // Do authentication...\n\n        $this-\u003egetRequest()-\u003esetAttribute(\"user_id\", 123);\n\n        return $next-\u003ehandle();\n    }\n}\n\n$app-\u003eaddMiddleware(new AuthMiddleware());\n\n// Or pass an array of middlewares to the App constructor\n$app = new \\JPI\\HTTP\\App($router, [new AuthMiddleware(), new AnotherMiddleware()]);\n```\n\n### Handling the Request\n\nOnce routes and middleware are configured, handle the incoming request and send the response:\n\n```php\n$response = $app-\u003ehandle();\n$response-\u003esend();\n```\n\n## API Reference\n\nFor a complete list of classes and methods, see the [API Reference](docs/API.md).\n\n## Support\n\nIf you found this library interesting or useful please spread the word about this library: share on your socials, star on GitHub, etc.\n\nIf you find any issues or have any feature requests, you can open a [issue](https://github.com/jahidulpabelislam/http/issues) or email [me @ jahidulpabelislam.com](mailto:me@jahidulpabelislam.com) :smirk:.\n\n## Authors\n\n- [Jahidul Pabel Islam](https://jahidulpabelislam.com/) [\u003cme@jahidulpabelislam.com\u003e](mailto:me@jahidulpabelislam.com)\n\n## License\n\nThis module is licensed under the General Public Licence - see the [licence](LICENSE.md) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjahidulpabelislam%2Fhttp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjahidulpabelislam%2Fhttp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjahidulpabelislam%2Fhttp/lists"}