{"id":15030421,"url":"https://github.com/evgsavosin/choco-router","last_synced_at":"2025-10-04T01:30:58.347Z","repository":{"id":56980186,"uuid":"364701738","full_name":"evgsavosin/choco-router","owner":"evgsavosin","description":"Modern and lightweight router for PHP, based on regex expression with cache system.","archived":true,"fork":false,"pushed_at":"2022-09-05T22:26:38.000Z","size":83,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-17T01:12:07.486Z","etag":null,"topics":["cache-routing","easy-to-use","fast-route","input-handler","library","lightweight","modern","php","php-router","php-router-standalone","php81","request-handler","route-parser","router","routing","routing-engine","simple-router","simple-routing","url-handler","url-parser"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/evgsavosin.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}},"created_at":"2021-05-05T20:46:17.000Z","updated_at":"2024-05-03T21:30:46.000Z","dependencies_parsed_at":"2022-08-21T10:50:26.236Z","dependency_job_id":null,"html_url":"https://github.com/evgsavosin/choco-router","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evgsavosin%2Fchoco-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evgsavosin%2Fchoco-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evgsavosin%2Fchoco-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evgsavosin%2Fchoco-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evgsavosin","download_url":"https://codeload.github.com/evgsavosin/choco-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235208940,"owners_count":18953003,"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":["cache-routing","easy-to-use","fast-route","input-handler","library","lightweight","modern","php","php-router","php-router-standalone","php81","request-handler","route-parser","router","routing","routing-engine","simple-router","simple-routing","url-handler","url-parser"],"created_at":"2024-09-24T20:13:17.475Z","updated_at":"2025-10-04T01:30:53.063Z","avatar_url":"https://github.com/evgsavosin.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Choco Router\n[![PHPUnit Tests](https://github.com/evgsavosin/choco-router/actions/workflows/phpunit.yml/badge.svg)](https://github.com/evgsavosin/choco-router/actions/workflows/phpunit.yml) \n[![Stable](https://poser.pugx.org/evgsavosin/choco-router/v/stable.svg)](https://packagist.org/packages/evgsavosin/choco-router)\n[![PHP](https://img.shields.io/badge/php-8.1-4f5b93.svg?logo=github)](https://img.shields.io/badge/php-8.1-4f5b93.svg?logo=github) \n[![License](https://poser.pugx.org/evgsavosin/choco-router/license.svg)](https://packagist.org/packages/evgsavosin/choco-router)\n\nModern router for PHP based on regex expression with cache system. Caching has driver support: file system, APCum, Memcached. Defining routes is possible both using methods and attributes from PHP 8.0.\n\n## Requirements\n- PHP 8.1 or newer;\n- APCu (optional);\n- Memcached (optional).\n\n## Install\nInstall via composer:\n```php\ncomposer require evgsavosin/choco-router\n```\n\n## Usage\n### Basic usage\nTo use it is necessary to define the call of classes using for example PSR-11 implementation.\n```php\n\u003c?php \n\ndeclare(strict_types=1);\n\nrequire 'vendor/autoload.php';\n\nuse ChocoRouter\\SimpleRouter;\nuse ChocoRouter\\HttpMethod;\n\n$router = new SimpleRouter();\n\n$router-\u003eaddRoute(HttpMethod::GET, '/foo', fn (): string =\u003e 'Foo!' );\n$router-\u003eaddRoute(\n    HttpMethod::POST, \n    '/foo/{bar}', \n    fn (mixed $value): string =\u003e \"Foo bar and {$value}!\",\n    ['bar' =\u003e '[0-9]+']\n);\n\ntry {\n    $router-\u003eresolve(\n        $_SERVER['REQUEST_METHOD'], \n        $_SERVER['REQUEST_URI']\n    )-\u003ecallableResolve(function (mixed $handler, array $arguments): mixed {\n        if (is_string($handler)) {\n            [$controllerName, $methodName] = explode('@', $handler);\n\n            // PSR-11 implementation for classes: controllers, actions and etc...\n        } elseif (is_callable($handler)) {\n            $handler(...$arguments);\n        }\n    }); \n} catch (HttpException $e) {\n    if ($e-\u003egetCode() === HttpException::NOT_FOUND) {\n        // Handle 404...\n    } else if ($e-\u003egetCode() === HttpException::BAD_REQUEST) {\n        // Handle 400...\n    }\n}\n```\n\n### Route definition\nThe route can be defined with method: `addRoute(HttpMethod $httpMethod, string $uri, mixed $handler, array $parameters = []): void`. Parameters can be passed `{bar}` or `{bar?}` with regular expressions `['bar' =\u003e '[0-9]+']`.\nReal example:\n```php\n$router-\u003eaddRoute(HttpMethod::GET, '/foo/{bar?}', 'foo-bar', ['bar' =\u003e '[0-9]+']);\n```\n\u003e A question mark means the parameter is optional.\n\nThe route group is defined using `addGroup(string $prefix, callable $callback): void` method. Real example: \n```php\n$router-\u003eaddGroup('/gorge', function (RouteCollection $r): void {\n    $router-\u003eaddRoute(HttpMethod::GET, '/foo/{bar?}', 'foo-bar', ['bar' =\u003e '[0-9]+']);\n});\n```\n\n### HTTP Methods\n\nFull list of methods:\n\n```php \nHttpMethod::CONNECT\nHttpMethod::HEAD\nHttpMethod::GET\nHttpMethod::POST\nHttpMethod::PUT\nHttpMethod::DELETE\nHttpMethod::OPTIONS\n```\n\n### Configuration\n\nYou can set the configuration when initializing a simple router:\n```php\n$router = new SimpleRouter([\n    'cacheDisable' =\u003e false,\n    'cacheDriver' =\u003e FileDriver::class,\n    'cacheOptions' =\u003e [] \n\n    /*\n        For memcached driver, there passed array of servers. \n        For file driver, there passed path to cache directory.\n    */\n]);\n```\n\n### Attributes\n\nThe router supports attributes from PHP 8.0. Example:\n\n```php \nuse App\\Action\\FooAction;\n\n$router = new SimpleRouter();\n$router-\u003eload([FooAction::class]);\n$router-\u003eresolve(/*...*/)-\u003ecallableResolve(/*...*/);\n```\n\n### Cache\n\nRouter has support cache system with defined drivers:\n- `ChocoRouter\\Cache\\Drivers\\FileDriver::class`;\n- `ChocoRouter\\Cache\\Drivers\\ApcuDriver::class`;\n- `ChocoRouter\\Cache\\Drivers\\MemcachedDriver::class`.\n\nFor use cache move definition routes to cache callback:\n```php \n$router = new SimpleRouter([\n    'cacheDriver' =\u003e FileDriver::class\n]);\n\n$router-\u003ecache(static function (RouteCollection $r): void {\n    $r-\u003eaddRoute(HttpMethod::GET, '/foo/{bar}', App\\Actions\\FooAction::class, ['bar' =\u003e '[0-9]+']);\n});\n\n$router-\u003eresolve(/*...*/)-\u003ecallableResolve(/*...*/);\n```\n\n## Contributing\n\nThe author has not yet had time to write instructions, but any pull request or issue will be glad.\n\n## License\n\nChoco Router has MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevgsavosin%2Fchoco-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevgsavosin%2Fchoco-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevgsavosin%2Fchoco-router/lists"}