{"id":22092105,"url":"https://github.com/krakphp/http","last_synced_at":"2025-08-26T16:25:57.419Z","repository":{"id":57009020,"uuid":"74574354","full_name":"krakphp/http","owner":"krakphp","description":"Http Utilities for PSR-7 Frameworks","archived":false,"fork":false,"pushed_at":"2017-05-14T06:37:17.000Z","size":97,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-01T10:36:54.914Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krakphp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-23T12:25:07.000Z","updated_at":"2017-03-12T10:07:44.000Z","dependencies_parsed_at":"2022-08-21T12:40:42.601Z","dependency_job_id":null,"html_url":"https://github.com/krakphp/http","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fhttp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fhttp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fhttp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fhttp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krakphp","download_url":"https://codeload.github.com/krakphp/http/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245186923,"owners_count":20574554,"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":[],"created_at":"2024-12-01T03:08:20.173Z","updated_at":"2025-03-23T23:44:53.564Z","avatar_url":"https://github.com/krakphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Http\n\nThe Krak Http package is a set of utilities for building Http applications. It comes with an implementation agnostic routing system, PSR-7 Response Factories, PSR-7 Server implementation, and a handful of useful middleware for Http applications.\n\n## Installation\n\nInstall via composer at `krak/http`\n\n## Usage\n\n### Response Factories\n\n```php\n\u003c?php\n\ninterface ResponseFactory {\n    public function createResponse($status = 200, array $headers = [], $body = null);\n}\n```\n\nEvery response factory must implement that interface.\n\n```php\n\u003c?php\n\nuse Krak\\Http\\ResponseFactory;\n\n$rf = new ResponseFactory\\DiactorosResponseFactory();\n$rf = new ResponseFactory\\GuzzleResponseFactory();\n\n// adds html content-type header\n$html_rf = new ResponseFactory\\HtmlResponseFactory($rf);\n// json encodes the body and add json content-type header. Accepts json_encode_options as second parameter\n$json_rf = new ResponseFactory\\JsonResponseFactory($rf, JSON_PRETTY_PRINT);\n// adds text content-type header\n$text_rf = new ResponseFactory\\TextResponseFactory($rf);\n\n$json_rf-\u003ecreateResponse(200, [], [1,2,3]);\n```\n\n### Routes\n\n```php\n\u003c?php\n\nuse Krak\\Http\\Route;\n\n$routes = new Route\\RouteGroup();\n$routes-\u003eget('/', function() {})-\u003ewith('attribute', 'value');\n$routes-\u003egroup('/foo', function($foo) {\n    $sub-\u003eget('', 'handler');\n    $sub-\u003egroup('/bar', function($bar) {\n        $bar-\u003eget('/baz', 'handler');\n    });\n});\n$routes-\u003ewith('attribute1', 'value');\n```\n\n### Compiling Routes\n\nOnce you've created a set of routes, you can then compile them with a route compiler. These will traverse the hierarchy of routes and flatten them into an iterator with normalized paths.\n\n```php\n\u003c?php\n\nuse Krak\\Http\\Route;\n\n$routes = new Route\\RouteGroup();\n// add routes to $routes\n\n$compiler = new Route\\RecursiveRouteCompiler();\n// compile on a path\n$routes = $compiler-\u003ecompileRoutes($routes, '/');\n```\n\n### Dispatch\n\nTo dispatch a set of routes, you need to create dispatcher factory, which will create a dispatcher from a set of routes, then you can dispatch a PSR-7 request.\n\n```php\n\u003c?php\n\nuse Krak\\Http\\Dispatcher;\n$dispatch_factory = new Dispatcher\\FastRoute\\FastRouteDispatcherFactory();\n$dispatch = $dispatch_factory-\u003ecreateDispatcher($routes);\n$res = $dispatch-\u003edispatch($req);\n\n// $res-\u003estatus_code\n// $res-\u003ematched_route-\u003eroute\n// $res-\u003ematched_route-\u003eparams\n// $res-\u003eallowed_methods /* if status code is a 405 response */\n```\n\n### Server\n\nThe server is responsible for creating a request, and emitting a response. It's a simple interface:\n\n```php\n\u003c?php\n\ninterface Server {\n    /** @param $handler resolves the request into a response object */\n    public function serve($handler);\n}\n```\n\n```php\n\u003c?php\n\n$server = new Krak\\Http\\Server\\DiactorosServer();\n$server-\u003eserve(function($req) {\n    return new Zend\\Diactoros\\Response();\n});\n```\n\n### Middleware\n\nHere are several useful middleware to use within your own applications. Each middleware takes two arguments: A PSR-7 Server Request, and an HttpLink. If you want more documentation on how the Link's work, checkout the Krak\\\\Mw library.\n\n```php\n\u003c?php\n\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse Krak\\Http\\Middleware\\HttpLink;\n\nfunction myMiddleware() {\n    return function(ServerRequestInterface $req, HttpLink $next) {\n        if (certainCondition($req)) {\n            return $next($req-\u003ewithAttribute('a', '1'))-\u003ewithStatusCode(404);\n        }\n\n        return $next-\u003eresponse(404, ['X-Header' =\u003e 'value'], 'Some body'); // can also use a php or psr-7 stream.\n    };\n}\n```\n\n#### injectRequestAttribute($name, $value)\n\nThis will automatically inject an attribute with a name and the given value.\n\n#### wrap($psr7_middleware)\n\nThis will wrap PSR-7 style middleware that use the request and response in the middleware parameters.\n\n```php\n\u003c?php\n\n$mw = Krak\\Http\\Middleware\\wrap(function($req, $resp, callable $next) {\n\n});\n```\n\n#### serveStatic($root)\n\nThis will sit and will check if a file exists at the URI path. If it does, it will serve the file, else it will fall through to the next middleware.\n\n#### mount($path, $mw)\n\nMounts a middleware on a path prefix. If the path prefix is matched, then the middleware is invoked.\n\n```php\n\u003c?php\n\nuse function Krak\\Http\\Middleware\\{mount, serveStatic};\n\n$mw = mount('/assets', serveStatic(__DIR__ . '/path/to/assets'));\n```\n\nThe above middleware will try to load files on the `/assets` uri. So `GET /assets/app.css` will return a css file content *if* `__DIR__ . /path/to/assets/app.css` exists in the filesystem.\n\n## Tests and Examples\n\nRun tests via:\n\n```bash\nmake test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fhttp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrakphp%2Fhttp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fhttp/lists"}