{"id":15296464,"url":"https://github.com/bermudaphp/router","last_synced_at":"2025-04-13T19:41:49.033Z","repository":{"id":56950729,"uuid":"263698811","full_name":"bermudaphp/router","owner":"bermudaphp","description":"SImple, flexible and very fast http-router with support psr-7 and psr-15","archived":false,"fork":false,"pushed_at":"2024-05-22T14:33:42.000Z","size":499,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"v4","last_synced_at":"2024-05-23T00:56:10.580Z","etag":null,"topics":["http-router","php","php-router","php8","php81","psr-15","psr-7","router","routing","uri","url"],"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/bermudaphp.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":"2020-05-13T17:29:27.000Z","updated_at":"2024-05-27T13:30:20.774Z","dependencies_parsed_at":"2024-01-08T04:31:33.399Z","dependency_job_id":"12ed16dd-0766-4746-be4e-c840821543a8","html_url":"https://github.com/bermudaphp/router","commit_stats":{"total_commits":428,"total_committers":2,"mean_commits":214.0,"dds":"0.24065420560747663","last_synced_commit":"e0505902f5ffc3e55f2a48339d290b9a56d71673"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bermudaphp%2Frouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bermudaphp%2Frouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bermudaphp%2Frouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bermudaphp%2Frouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bermudaphp","download_url":"https://codeload.github.com/bermudaphp/router/tar.gz/refs/heads/v4","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248770745,"owners_count":21159028,"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-router","php","php-router","php8","php81","psr-15","psr-7","router","routing","uri","url"],"created_at":"2024-09-30T18:10:37.366Z","updated_at":"2025-04-13T19:41:49.026Z","avatar_url":"https://github.com/bermudaphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":" # Installation\n ```bash\n composer require bermudaphp/router\n ````\n ## Usage\n\n ```php\n use Bermuda\\Router\\Routes;\n\n $routes = new Routes;\n $router = Router::fromDnf($routes);\n\n $routes-\u003eaddRoute(\n     RouteRecord::get('home', '/hello/[name]', static function(string $name): void {\n         echo sprintf('Hello, %s!', $name)\n     })\n ); \n \n $route = $router-\u003ematch($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);\n if (!route) {\n     // route not found logics\n }\n \n call_user_func($route-\u003ehandler, $route-\u003eparams['name']);\n ```\n ## Route path generation\n ```php\n echo $router-\u003egenerate('home', ['name' =\u003e 'Jane Doe']); // Output /hello/Jane%20Doe\n ```\n ## Usage with PSR-15\n \n ```php\n \n $pipeline = new \\Bermuda\\Pipeline\\Pipeline();\n $factory = new \\Bermuda\\MiddlewareFactory\\MiddlewareFactory($container, $responseFactory);\n \n class Handler implements RequestHandlerInterface\n {\n    public function handle(ServerRequestInterface $request): ResponseInterface\n    {\n        return new TextResponse(sprintf('Hello, %s!', $request-\u003egetAttribute('name')))\n    }\n };\n \n $routes-\u003eaddRoute(\n     RouteRecord::get('home', '/hello/[name:[a-z]]', Handler::class)\n ); \n \n $pipeline-\u003epipe($factory-\u003emake(Bermuda\\Router\\Middleware\\MatchRouteMiddleware::class));\n $pipeline-\u003epipe($factory-\u003emake(Bermuda\\Router\\Middleware\\DispatchRouteMiddleware::class)\n     -\u003esetFallbackHandler($container-\u003eget(Bermuda\\Router\\Middleware\\RouteNotFoundHandler::class)));\n  \n $response = $pipeline-\u003ehandle($request);\n\n send($response)\n ```\n ## Get current route data\n \n ```php\n class Handler implements RequestHandlerInterface\n {\n    public function handle(ServerRequestInterface $request): ResponseInterface\n    {\n        $route = $request-\u003egetAttribute('Bermuda\\Router\\Middleware\\RouteMiddleware')-\u003eroute; // MatchedRoute instance\n    }\n }; \n ```\n ## RouteRecord HTTP verbs helpers\n \n ```php\n RouteRecord::get(string $name, string $path, mixed $handler): RouteRecord ;\n RouteRecord::post(string $name, string $path, mixed $handler): RouteRecord ;\n RouteRecord::patch(string $name, string $path, mixed $handler): RouteRecord ;\n RouteRecord::put(string $name, string $path, mixed $handler): RouteRecord ;\n RouteRecord::delete(string $name, string $path, mixed $handler): RouteRecord ;\n RouteRecord::options(string $name, string $path, mixed $handler): RouteRecord ;\n RouteRecord::head(string $name, string $path, mixed $handler): RouteRecord ;\n RouteRecord::any(string $name, string $path, mixed $handler, array $methods = ['GET', 'POST', 'PUT', 'PATCH', 'OPTIONS', 'HEAD', 'DELETE']): RouteRecord\n ```\n \n ## Set attribute placeholder pattern\n \n ```php\n $routes-\u003eaddRoute(RouteRecord::get('users.get, '/api/v1/users/[id:[a-zA-Z]]', static function(ServerRequestInterface $request): ResponseInterface {\n     return findUser($request-\u003egetAttribute('id'));\n }));\n\n alternative:\n $routes-\u003eaddRoute(RouteRecord::get('users.get, '/api/v1/users/[id]', static function(ServerRequestInterface $request): ResponseInterface {\n     return findUserById($request-\u003egetAttribute('id'));\n })-\u003ewithToken('id', '[a-zA-Z]'));\n ```\n ## Optional attribute\n \n ```php\n $routes-\u003eaddRoute(RouteRecord::get('users.get, '/api/v1/users/[?id]', static function(ServerRequestInterface $request): ResponseInterface {\n     if (($id = $request-\u003egetAttribute('id')) !== null) {\n         return findUserById($id);\n     }\n     \n     return get_all_users();\n }));\n ```\n \n ## Predefined placeholders\n \n ````\n id: \\d+\n any: .*\n ````\n \n Other placeholders passed to path as a string without being explicitly defined will match the pattern `.+`\n  \n ## Routes Group\n \n ```php\n $group = $routes-\u003egroup(name: 'api', prifix: '/api'); // set routes group\n\n$group-\u003eaddRoute(RouteRecord::get('users.get, 'users/[?id]', GetUserHandler::class));\n $group-\u003eaddRoute(RouteRecord::post(user.create, 'users', CreateUserHandler::class));\n\n $group-\u003esetMiddleware([GuardMiddleware::class]) // set middleware for all routes in group\n $group-\u003esetTokens(['id' =\u003e '[a-zA-Z]']) // set tokens for all routes in group\n\n\n $group = $routes-\u003egroup('api') // get routes group from name\n \n ```\n\n## Cache\n \nOnce all routes are registered in the route map and they will no longer be changed. Call the $routes-\u003ecache method to cache the route map in a php file. Then use the `Routes::createFromCache('/path/to/cached/routes/filename.php')` method to create a map instance with preloaded routes.\n\n```php\n \n $routes-\u003ecache('path/to/cached/routes/file.php');\n $routes = Routes::createFromCache('path/to/cached/routes/file.php')\n \n $router = Router::fromDnf($routes);\n ```\n# Cache context\nIf you are using a parent-context-bound closure (the use construct) as a route handler, then you must pass an array of bound variables to the `Routes::createFromCache` method. See example below\n```php\n $app = new App;\n $repository = new UserRepository;\n $routes-\u003eaddRoute(RouteRecord::get('user.get', '/users/[id]', static function(ServerRequest $request) use ($app, $repository): ResponseInterface {\n    return $app-\u003erespond(200, $repository-\u003efindById($request-\u003egetAttribute('id')));\n }));\n\n $routes-\u003ecache('path/to/cached/routes/file.php');\n $routes = Routes::createFromCache('path/to/cached/routes/file.php', compact('app', 'repository'));\n ```\n \n # Cache limitations\n Currently, the caching implementation does not allow caching routes using object instances and callback functions based on object instances.\n\n # Benchmark\n ```\n+---------------------------+-------------------+------------+-----------------+-------+--------------+-------------------+\n| benchmark                 | registered_routes | cache_mode | exec_time       | its   | memory_usage | memory_peak_usage |\n+---------------------------+-------------------+------------+-----------------+-------+--------------+-------------------+\n| Benchmark\\RouterBenchmark | 1001              | disable    | 32.680938005447 | 10000 | 16 MB        | 16 MB             |\n| Benchmark\\RouterBenchmark | 1001              | enable     | 1.298574924469  | 10000 | 16 MB        | 16 MB             |\n+---------------------------+-------------------+------------+-----------------+-------+--------------+-------------------+\n ````\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbermudaphp%2Frouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbermudaphp%2Frouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbermudaphp%2Frouter/lists"}