{"id":21467383,"url":"https://github.com/elusivecodes/fyrerouter","last_synced_at":"2025-07-15T05:31:24.214Z","repository":{"id":56982788,"uuid":"441913697","full_name":"elusivecodes/FyreRouter","owner":"elusivecodes","description":"FyreRouter is a free, open-source URL routing library for PHP.","archived":false,"fork":false,"pushed_at":"2024-10-18T12:20:49.000Z","size":233,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-22T16:59:12.401Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elusivecodes.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":"2021-12-26T14:43:42.000Z","updated_at":"2024-10-18T12:19:57.000Z","dependencies_parsed_at":"2024-04-12T12:27:36.463Z","dependency_job_id":"647b0ae0-750a-46ae-8f4b-90118d231023","html_url":"https://github.com/elusivecodes/FyreRouter","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"1a359046c1725731c50b4a9b1d4197ed2c80c98a"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elusivecodes%2FFyreRouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elusivecodes%2FFyreRouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elusivecodes%2FFyreRouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elusivecodes%2FFyreRouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elusivecodes","download_url":"https://codeload.github.com/elusivecodes/FyreRouter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226017441,"owners_count":17560509,"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-11-23T08:17:49.376Z","updated_at":"2025-07-15T05:31:24.204Z","avatar_url":"https://github.com/elusivecodes.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FyreRouter\r\n\r\n**FyreRouter** is a free, open-source URI routing library for *PHP*.\r\n\r\n\r\n## Table Of Contents\r\n- [Installation](#installation)\r\n- [Basic Usage](#basic-usage)\r\n- [Methods](#methods)\r\n- [Routes](#routes)\r\n    - [Closure](#closure)\r\n    - [Controller](#controller)\r\n    - [Redirect](#redirect)\r\n- [Middleware](#middleware)\r\n    - [Substitute Bindings](#substitute-bindings)\r\n\r\n\r\n\r\n## Installation\r\n\r\n**Using Composer**\r\n\r\n```\r\ncomposer require fyre/router\r\n```\r\n\r\nIn PHP:\r\n\r\n```php\r\nuse Fyre\\Router\\Router;\r\n```\r\n\r\n\r\n## Basic Usage\r\n\r\n- `$container` is a [*Container*](https://github.com/elusivecodes/FyreContainer).\r\n- `$modelRegistry` is a [*ModelRegistry*](https://github.com/elusivecodes/FyreORM).\r\n- `$config` is a [*Config*](https://github.com/elusivecodes/FyreConfig).\r\n\r\n```php\r\n$router = new Router($container, $modelRegistry, $config);\r\n```\r\n\r\nThe base URI will be resolved from the \"*App.baseUri*\" key in the [*Config*](https://github.com/elusivecodes/FyreConfig).\r\n\r\n**Autoloading**\r\n\r\nIt is recommended to bind the *Router* to the [*Container*](https://github.com/elusivecodes/FyreContainer) as a singleton.\r\n\r\n```php\r\n$container-\u003esingleton(Router::class);\r\n```\r\n\r\nAny dependencies will be injected automatically when loading from the [*Container*](https://github.com/elusivecodes/FyreContainer).\r\n\r\n```php\r\n$router = $container-\u003euse(Router::class);\r\n```\r\n\r\n\r\n## Methods\r\n\r\n**Clear**\r\n\r\nClear all routes and aliases.\r\n\r\n```php\r\n$router-\u003eclear();\r\n```\r\n\r\n**Connect**\r\n\r\nConnect a route.\r\n\r\n- `$path` is a string representing the route path, and can include placeholders (that will be passed to the destination).\r\n- `$destination` can be either a string representing the destination, an array containing the class name and method or a *Closure*.\r\n- `$options` is an array containing configuration options.\r\n    - `as` is a string representing the route alias, and will default to *null*.\r\n    - `middleware` is an array of middleware to be applied to the route, and will default to *[]*.\r\n    - `method` is an array of strings representing the matching methods, and will default to *[]*.\r\n    - `placeholders` is an array of regular expression placeholders, and will default to *[]*.\r\n    - `redirect` is a boolean indicating whether the route is a redirect, and will default to *false*.\r\n\r\n```php\r\n$route = $router-\u003econnect($path, $destination, $options);\r\n```\r\n\r\nYou can generate the following helper methods to connect specific routes.\r\n\r\n```php\r\n$router-\u003edelete($path, $destination, $options);\r\n$router-\u003eget($path, $destination, $options);\r\n$router-\u003epatch($path, $destination, $options);\r\n$router-\u003epost($path, $destination, $options);\r\n$router-\u003eput($path, $destination, $options);\r\n$router-\u003eredirect($path, $destination, $options);\r\n```\r\n\r\nSee the [Routes](#routes) section for supported path and destination formats.\r\n\r\nYou can also pass additional arguments to the middleware by appending a colon followed by a comma-separated list of arguments to the alias string. You can use route placeholders as arguments by referencing the route placeholder surrounded by curly braces.\r\n\r\n```php\r\n$router-\u003eget('test/{id}', 'test', ['middleware' =\u003e 'alias:test,{id}']);\r\n```\r\n\r\n**Get Base Uri**\r\n\r\nGet the base uri.\r\n\r\n```php\r\n$baseUri = $router-\u003egetBaseUri();\r\n```\r\n\r\n**Group**\r\n\r\nCreate a group of routes.\r\n\r\n- `$options` is an array containing the group options.\r\n    - `prefix` is a string representing the route group path prefix, and will default to *null*.\r\n    - `as` is a string representing the route group alias prefix, and will default to *null*.\r\n    - `middleware` is an array of middleware to be applied to the route group, and will default to *[]*.\r\n    - `placeholders` is an array of regular expression placeholders, and will default to *[]*.\r\n- `$callback` is a *Closure* with the *Router* as the first argument.\r\n\r\n```php\r\n$router-\u003egroup($options, $callback);\r\n```\r\n\r\n**Load Route**\r\n\r\nLoad a route.\r\n\r\n- `$request` is a [*ServerRequest*](https://github.com/elusivecodes/FyreServer#server-requests).\r\n\r\n```php\r\n$request = $router-\u003eloadRoute($request);\r\n```\r\n\r\nThis method will return a [*ServerRequest*](https://github.com/elusivecodes/FyreServer#server-requests), with the `route` parameter set to the loaded route.\r\n\r\n**Url**\r\n\r\nGenerate a URL for a named route.\r\n\r\n- `$name` is a string representing the route alias.\r\n- `$arguments` is an array containing the route arguments, where the key is the placeholder name.\r\n    - `?` is an array containing route query parameters.\r\n    - `#` is a string representing the fragment component of the URI.\r\n- `$options` is an array containing the route options.\r\n    - `fullBase` is a boolean indicating whether to use the full base URI and will default to *false*.\r\n\r\n```php\r\n$url = $router-\u003eurl($name, $arguments, $options)\r\n```\r\n\r\n\r\n## Routes\r\n\r\nAll routes extend the `Fyre\\Router\\Route` class, and include the following methods.\r\n\r\n**Check Method**\r\n\r\nCheck if the route matches a test method.\r\n\r\n- `$method` is a string representing the method to test.\r\n\r\n```php\r\n$checkMethod = $route-\u003echeckMethod($method);\r\n```\r\n\r\n**Check Path**\r\n\r\nCheck if the route matches a test path.\r\n\r\n- `$path` is a string representing the path to test.\r\n\r\n```php\r\n$checkPath = $route-\u003echeckPath($path);\r\n```\r\n\r\n**Get Arguments**\r\n\r\nGet the route arguments.\r\n\r\n```php\r\n$arguments = $route-\u003egetArguments();\r\n```\r\n\r\n**Get Binding Fields**\r\n\r\nGet the route binding fields.\r\n\r\n```php\r\n$bindingFields = $route-\u003egetBindingFields();\r\n```\r\n\r\n**Get Destination**\r\n\r\nGet the route destination.\r\n\r\n```php\r\n$destination = $route-\u003egetDestination();\r\n```\r\n\r\n**Get Middleware**\r\n\r\nGet the route middleware.\r\n\r\n```php\r\n$middleware = $route-\u003egetMiddleware();\r\n```\r\n\r\n**Get Path**\r\n\r\nGet the route path.\r\n\r\n```php\r\n$path = $route-\u003egetPath();\r\n```\r\n\r\n**Get Placeholders**\r\n\r\nGet the route placeholders.\r\n\r\n```php\r\n$placeholders = $route-\u003egetPlaceholders();\r\n```\r\n\r\n**Handle**\r\n\r\nHandle the route.\r\n\r\n- `$request` is a [*ServerRequest*](https://github.com/elusivecodes/FyreServer#server-requests).\r\n- `$response` is a [*ClientResponse*](https://github.com/elusivecodes/FyreServer#client-responses).\r\n\r\n```php\r\n$response = $route-\u003ehandle($request, $response);\r\n```\r\n\r\nThis method will return a [*ClientResponse*](https://github.com/elusivecodes/FyreServer#client-responses).\r\n\r\n**Set Middleware**\r\n\r\nSet the route middleware.\r\n\r\n- `$middleware` is an array containing the route middleware.\r\n\r\n```php\r\n$route-\u003esetMiddleware($middleware);\r\n```\r\n\r\n**Set Placeholder**\r\n\r\nSet a route placeholder.\r\n\r\n- `$placeholder` is a string representing the route placeholder.\r\n- `$regex` is a string representing the placeholder regular expression.\r\n\r\n```php\r\n$route-\u003esetPlaceholder($placeholder, $regex);\r\n```\r\n\r\n\r\n### Closure\r\n\r\n```php\r\nuse Fyre\\Router\\Routes\\ClosureRoute;\r\n```\r\n\r\n- `$container` is a [*Container*](https://github.com/elusivecodes/FyreContainer).\r\n- `$destination` is a *Closure*.\r\n- `$path` is a string representing the route path, and will default to \"\".\r\n- `$options` is an array containing route options.\r\n    - `middleware` is an array of middleware to be applied to the route, and will default to *[]*.\r\n    - `method` is an array of strings representing the matching methods, and will default to *[]*.\r\n    - `placeholders` is an array of regular expression placeholders, and will default to *[]*.\r\n\r\n```php\r\n$route = new ClosureRoute($container, $destination, $path, $options);\r\n```\r\n\r\nThe `$path` and `$destination` can be expressed in the following formats:\r\n\r\n```php\r\n$router-\u003eget('posts', function(): string {\r\n    return view('Posts.index');\r\n});\r\n\r\n$router-\u003eget('posts/{post}', function(Post $post): string {\r\n    return view('Posts.view', ['post' =\u003e $post]);\r\n}); \r\n```\r\n\r\nRoute parameter entity binding is handled by the [Substitute Bindings](#substitute-bindings) middleware.\r\n\r\n\r\n### Controller\r\n\r\n```php\r\nuse Fyre\\Router\\Routes\\ControllerRoute;\r\n```\r\n\r\n- `$container` is a [*Container*](https://github.com/elusivecodes/FyreContainer).\r\n- `$destination` is an array containing the controller class name and method.\r\n- `$path` is a string representing the route path, and will default to \"\".\r\n- `$options` is an array containing route options.\r\n    - `middleware` is an array of middleware to be applied to the route, and will default to *[]*.\r\n    - `method` is an array of strings representing the matching methods, and will default to *[]*.\r\n    - `placeholders` is an array of regular expression placeholders, and will default to *[]*.\r\n\r\n```php\r\n$route = new ControllerRoute($container, $destination, $path, $options);\r\n```\r\n\r\nThe `$path` and `$destination` can be expressed in the following formats:\r\n\r\n```php\r\n$router-\u003eget('posts', [Posts::class]); // defaults to index method\r\n$router-\u003eget('posts/{post}', [Posts::class, 'view']);\r\n```\r\n\r\nRoute parameter entity binding is handled by the [Substitute Bindings](#substitute-bindings) middleware.\r\n\r\n**Get Action**\r\n\r\nGet the route controller action.\r\n\r\n```php\r\n$action = $route-\u003egetAction();\r\n```\r\n\r\n**Get Controller**\r\n\r\nGet the route controller class name.\r\n\r\n```php\r\n$controller = $route-\u003egetController();\r\n```\r\n\r\n\r\n### Redirect\r\n\r\n```php\r\nuse Fyre\\Router\\Routes\\RedirectRoute;\r\n```\r\n\r\n- `$container` is a [*Container*](https://github.com/elusivecodes/FyreContainer).\r\n- `$destination` is a string representing the destination.\r\n- `$path` is a string representing the route path, and will default to \"\".\r\n- `$options` is an array containing route options.\r\n    - `middleware` is an array of middleware to be applied to the route, and will default to *[]*.\r\n    - `method` is an array of strings representing the matching methods, and will default to *[]*.\r\n    - `placeholders` is an array of regular expression placeholders, and will default to *[]*.\r\n\r\n```php\r\n$route = new RedirectRoute($container, $destination, $path, $options);\r\n```\r\n\r\nThe `$path` and `$destination` can be expressed in the following formats:\r\n\r\n```php\r\n$router-\u003eredirect('test', 'https://test.com/');\r\n$router-\u003eredirect('test/{id}', 'https://test.com/{id}');\r\n```\r\n\r\n\r\n## Middleware\r\n\r\n```php\r\nuse Fyre\\Router\\Middleware\\RouterMiddleware;\r\n```\r\n\r\n- `$container` is a [*Container*](https://github.com/elusivecodes/FyreContainer).\r\n- `$middlewareRegistry` is a [*MiddlewareRegistry*](https://github.com/elusivecodes/FyreMiddleware).\r\n- `$router` is a *Router*.\r\n\r\n```php\r\n$middleware = new RouterMiddleware($container, $middlewareRegistry, $router);\r\n```\r\n\r\nAny dependencies will be injected automatically when loading from the [*Container*](https://github.com/elusivecodes/FyreContainer).\r\n\r\n```php\r\n$middleware = $container-\u003euse(RouterMiddleware::class);\r\n```\r\n\r\n**Handle**\r\n\r\nHandle a [*ServerRequest*](https://github.com/elusivecodes/FyreServer#server-requests).\r\n\r\n- `$request` is a [*ServerRequest*](https://github.com/elusivecodes/FyreServer#server-requests).\r\n- `$next` is a *Closure*.\r\n\r\n```php\r\n$response = $middleware-\u003ehandle($request, $next);\r\n```\r\n\r\nThis method will return a [*ClientResponse*](https://github.com/elusivecodes/FyreServer#client-responses).\r\n\r\n\r\n### Substitute Bindings\r\n\r\n```php\r\nuse Fyre\\Router\\Middleware\\SubstituteBindingsMiddleware;\r\n```\r\n\r\nThis middleware will automatically resolve entities from route placeholders based on the parameter types of the route destination.\r\n\r\n- `$container` is a [*Container*](https://github.com/elusivecodes/FyreContainer).\r\n- `$middlewareRegistry` is a [*MiddlewareRegistry*](https://github.com/elusivecodes/FyreMiddleware).\r\n- `$entityLocator` is an [*EntityLocator*](https://github.com/elusivecodes/FyreEntity).\r\n\r\n```php\r\n$middleware = new SubstituteBindingsMiddleware($container, $middlewareRegistry, $entityLocator);\r\n```\r\n\r\nAny dependencies will be injected automatically when loading from the [*Container*](https://github.com/elusivecodes/FyreContainer).\r\n\r\n```php\r\n$middleware = $container-\u003euse(SubstituteBindingsMiddleware::class);\r\n```\r\n\r\n**Handle**\r\n\r\nHandle a [*ServerRequest*](https://github.com/elusivecodes/FyreServer#server-requests).\r\n\r\n- `$request` is a [*ServerRequest*](https://github.com/elusivecodes/FyreServer#server-requests).\r\n- `$next` is a *Closure*.\r\n\r\n```php\r\n$response = $middleware-\u003ehandle($request, $next);\r\n```\r\n\r\nThis method will return a [*ClientResponse*](https://github.com/elusivecodes/FyreServer#client-responses).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felusivecodes%2Ffyrerouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felusivecodes%2Ffyrerouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felusivecodes%2Ffyrerouter/lists"}