{"id":15096868,"url":"https://github.com/incapption-public/simpleapi","last_synced_at":"2026-02-12T12:49:26.414Z","repository":{"id":56990830,"uuid":"410887798","full_name":"incapption-public/SimpleApi","owner":"incapption-public","description":"Simple API Framework for PHP","archived":false,"fork":false,"pushed_at":"2024-01-30T17:40:11.000Z","size":59,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-27T16:19:23.196Z","etag":null,"topics":["api-rest","composer","php-library","php72"],"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/incapption-public.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-09-27T13:07:09.000Z","updated_at":"2024-06-22T06:02:26.000Z","dependencies_parsed_at":"2023-12-05T16:10:14.125Z","dependency_job_id":"38ef6413-be9c-4a05-afb6-9f17bf7ca3c0","html_url":"https://github.com/incapption-public/SimpleApi","commit_stats":{"total_commits":44,"total_committers":2,"mean_commits":22.0,"dds":"0.13636363636363635","last_synced_commit":"9b3285bca9daf99a7629d6f80ec0f712aca01f05"},"previous_names":["incapption-public/simpleapi"],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/incapption-public%2FSimpleApi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/incapption-public%2FSimpleApi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/incapption-public%2FSimpleApi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/incapption-public%2FSimpleApi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/incapption-public","download_url":"https://codeload.github.com/incapption-public/SimpleApi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245878929,"owners_count":20687297,"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":["api-rest","composer","php-library","php72"],"created_at":"2024-09-25T16:01:58.377Z","updated_at":"2026-02-12T12:49:26.358Z","avatar_url":"https://github.com/incapption-public.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SimpleApi\n\nTiny package for setting up a simple REST API with PHP.  \nAbility to group routes and define middlewares on a group or route level.\n\n## Usage\n\n1. Implement the iApiController interface with your controller. A method can return `StringResult`, `DataResult` or `ErrorResult` which always return arrays with different structure based on the result.\n\n```php\n\u003c?php\n\nuse Incapption\\SimpleApi\\Models\\ApiRequest;\nuse Incapption\\SimpleApi\\Models\\StringResult;\nuse Incapption\\SimpleApi\\Enums\\HttpStatusCode;\nuse Incapption\\SimpleApi\\Interfaces\\iMethodResult;\nuse Incapption\\SimpleApi\\Interfaces\\iApiController;\n\nclass TestController implements iApiController\n{\n  public function get(ApiRequest $request): iMethodResult\n  {\n     return new StringResult(HttpStatusCode::SUCCESS(), 'TestController-\u003eget()');\n  }\n\n  public function index(ApiRequest $request): iMethodResult\n  {\n    return new StringResult(HttpStatusCode::SUCCESS(), 'TestController-\u003eindex()');\n  }\n\n  public function create(ApiRequest $request): iMethodResult\n  {\n    return new StringResult(HttpStatusCode::SUCCESS(), 'TestController-\u003ecreate()');\n  }\n\n  public function update(ApiRequest $request): iMethodResult\n  {\n    return new StringResult(HttpStatusCode::SUCCESS(), 'TestController-\u003eupdate()');\n  }\n\n  public function delete(ApiRequest $request): iMethodResult\n  {\n    return new StringResult(HttpStatusCode::SUCCESS(), 'TestController-\u003edelete()');\n  }\n}\n```\n\n2. Extend the `SimpleApi` class. Here you can group and register your routes.\n\n```php\n\u003c?php\n\nuse Incapption\\SimpleApi\\SimpleApi;\nuse Incapption\\SimpleApi\\SimpleApiRoute;\nuse Incapption\\SimpleApi\\Tests\\Controllers\\TestController;\nuse Incapption\\SimpleApi\\Tests\\Middleware\\AuthenticationMiddleware;\n\nclass SimpleApiExtension extends SimpleApi\n{\n  public function __construct(string $requestUri, string $requestMethod, array $headers = [], array $input = [])\n  {\n    parent::__construct($requestUri, $requestMethod, $headers, $input);\n    $this-\u003eregisterRoutes();\n  }\n\n  protected function registerRoutes()\n  {\n    // A group which always requires authentication\n    SimpleApiRoute::registerGroup('user', [new AuthenticationMiddleware()]);\n\n    // A public endpoint not requiring authentication. The 'public' group is defined without middleware.\n    SimpleApiRoute::registerGet('public', '/api/currencies', TestController::class, 'get');\n\n    // The middleware defined for group 'user' above will be executed when calling this route.\n    SimpleApiRoute::registerGet('user', '/api/user/{userId}/files', TestController::class, 'get');\n\n    // Example for a group which might require user authentication middleware\n    SimpleApiRoute::registerGet('internal', '/api/schema', TestController::class, 'get', [new AuthenticationMiddleware()]);\n  }\n}\n```\n\n3. In your application root (e.g. app.php) use this controller to verify if the request is an API request, call the method and return the result.\n\n```php\nuse Incapption\\SimpleApi\\Helper\\HttpHeader;\nuse Incapption\\SimpleApi\\Helper\\HttpPayload;use Incapption\\SimpleApi\\Tests\\SimpleApiExtension;\n\n$api = new SimpleApiExtension($_SERVER[\"REQUEST_URI\"], $_SERVER[\"REQUEST_METHOD\"], HttpHeader::getAll(), HttpPayload::getInput());\n\n// Check if the actual request is targeted at your API endpoint\nif ($api-\u003eisApiEndpoint('/api/v1'))\n{\n  $result = $api-\u003egetResult();\n  // Returns the result as JSON and exits the application\n  $api-\u003esendAndTerminate($result);\n}\n```\n\nThe returned JSON format is like this depending on whether the Controller method returns a `StringResult`, `DataResult` or `ErrorResult` object:\n\nExample for `DataResult` which returns an object of unknown structure.\n```json\n{\n  \"name\": \"John Doe\",\n  \"age\": 27\n}\n```\n\nExample for return type `StringResult`, which always returns a `message` of type `string`\n```json\n{\n  \"message\": \"This request was a success\"\n}\n```\n\nExample for return type `ErrorResult`, which always contains an `errors` key which contains either a string or list of errors.\n```json\n{\n  \"errors\": \"User ID is required\"\n}\n```\n\n```json\n{\n  \"errors\": {\n     \"user_id\": [\n        \"Must be of type string\"\n     ]\n  }\n}\n```\n\n## Tests\n\nCreate a `phpunit.xml` in the project directory. Run PHPUnit referencing this xml. For example:\n`php.exe ./SimpleApi/vendor/phpunit/phpunit/phpunit --configuration .\\SimpleApi\\phpunit.xml --teamcity`\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cphpunit xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" bootstrap=\"./vendor/autoload.php\"\n         convertErrorsToExceptions=\"true\" convertNoticesToExceptions=\"true\" convertWarningsToExceptions=\"true\"\n         processIsolation=\"false\" stopOnFailure=\"true\"\n         xsi:noNamespaceSchemaLocation=\"https://schema.phpunit.de/9.5/phpunit.xsd\"\u003e\n    \u003ccoverage\u003e\n        \u003cinclude\u003e\n            \u003cdirectory suffix=\".php\"\u003esrc/\u003c/directory\u003e\n        \u003c/include\u003e\n    \u003c/coverage\u003e\n    \u003ctestsuites\u003e\n        \u003ctestsuite name=\"SimpleApi Test Suite\"\u003e\n            \u003cdirectory\u003etests\u003c/directory\u003e\n        \u003c/testsuite\u003e\n    \u003c/testsuites\u003e\n\u003c/phpunit\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fincapption-public%2Fsimpleapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fincapption-public%2Fsimpleapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fincapption-public%2Fsimpleapi/lists"}