{"id":20418256,"url":"https://github.com/askonomm/router","last_synced_at":"2025-04-12T17:25:23.513Z","repository":{"id":193728414,"uuid":"689100807","full_name":"askonomm/router","owner":"askonomm","description":"A minimal PHP router with configuration-free dependency injection.","archived":false,"fork":false,"pushed_at":"2024-05-30T22:31:59.000Z","size":41,"stargazers_count":13,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T12:51:14.226Z","etag":null,"topics":["dependency-injection","php","router"],"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/askonomm.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":"2023-09-08T19:59:01.000Z","updated_at":"2024-12-09T15:14:26.000Z","dependencies_parsed_at":"2024-11-21T03:31:13.496Z","dependency_job_id":null,"html_url":"https://github.com/askonomm/router","commit_stats":{"total_commits":27,"total_committers":1,"mean_commits":27.0,"dds":0.0,"last_synced_commit":"71b3f9f284f6997f00505a3784129de11db81807"},"previous_names":["askonomm/router"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/askonomm%2Frouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/askonomm%2Frouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/askonomm%2Frouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/askonomm%2Frouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/askonomm","download_url":"https://codeload.github.com/askonomm/router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248603290,"owners_count":21131770,"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":["dependency-injection","php","router"],"created_at":"2024-11-15T06:30:49.939Z","updated_at":"2025-04-12T17:25:23.486Z","avatar_url":"https://github.com/askonomm.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Router\n\n[![codecov](https://codecov.io/gh/askonomm/router/graph/badge.svg?token=7UTJN1TK6S)](https://codecov.io/gh/askonomm/router)\n\nA PHP router with built-in configuration-free dependency injection.\n\n## Install\n\n```\ncomposer require asko/router\n```\n\n## Usage\n\nThe most basic usage example looks like this:\n\n```php\nuse Asko\\Router\\Router;\n\n$router = new Router();\n\n$router-\u003eget(\"/hello/{who}\", function(string $who) {\n    echo \"Hello, {$who}\";\n});\n\n$router-\u003edispatch();\n```\n\nAll HTTP methods are supported: `get`, `head`, `post`, `put`, `delete`, `patch`, `options`, `trace`. To catch any HTTP method, use `any`.\n\nTo set a 404 handler, use the `not_found` method, which just takes a callable as its only argument.\n\n### Callables\n\nYou can pass 3 different types of callables to the router.\n\n#### Controllers\n\nControllers are classes which are mostly used for grouping routes that are similar together, so let's say you have Admin page routes, well, it would make sense to create a `AdminController` class for that, where each method in it represents a single route. To use controller classes with Router you need to pass an array as the callable, where the first item is the class constant and the second the name of the method, like so:\n\n```php\nuse Asko\\Router\\Router;\n\nclass AdminController\n{\n    public function login()\n    {\n        echo \"Login page goes here.\";\n    }\n}\n\n$router = new Router();\n$router-\u003eget(\"/admin/login\", [AdminController::class, \"login\"]);\n$router-\u003edispatch();\n```\n\n#### Functions\n\nFunctions are regular PHP functions where you pass the name of the function as the callable. This is useful if you want to do more functional style programming, like so:\n\n```php\nuse Asko\\Router\\Router;\n\nfunction hello_world() {\n    echo \"Hello, World!\";\n}\n\n$router = new Router();\n$router-\u003eget(\"/hello-world\", \"hello_world\");\n$router-\u003edispatch();\n```\n\n#### Closures\n\nYou can also entirely forgo having named functions and instead use anonymous functions in the form of Closures, like so:\n\n```php\nuse Asko\\Router\\Router;\n\n$router = new Router();\n\n$router-\u003eget(\"/hello-world\", function() {\n    echo \"Hello, World!\";\n});\n\n$router-\u003edispatch();\n```\n\n### Parameters\n\nParameters in Router are named, and then used in the function (or method) declaration as arguments with those same names. So let's say you have this Route:\n\n```php\n$router-\u003eget(\"/hello/{who}\", ...);\n```\n\nThen the argument name you need to refer to is also `$who`, like so:\n\n```php\n$router-\u003eget(\"/hello/{who}\", function(string $who) {\n    echo \"Hello, {$who}!\";\n});\n```\n\nYou can have as many parameters as you wish, and the order of which you have them in the function declaration does not matter. The only thing that matters is that the name matches the Route parameter.\n\n**Note:** your parameters must be type hinted as either `string`, `int` or `float`. Leaving parameters untyped will assume the parameter is `string`.\n\n### Dependency injection\n\nRouter has configuration-free dependency injection in the form of type hinting classes in the function (or method) declaration. Dependency injections must occur before Route parameters. An example injection looks like this:\n\n```php\nuse Asko\\Router\\Router;\n\nclass SomeDependency {}\n\n$router = new Router();\n\n$router-\u003eget(\"/hello/{who}\", function(SomeDependency $dep, string $who) {\n    echo \"Hello, {$who}\";\n});\n\n$router-\u003edispatch();\n```\n\nThe above examples instantiates the `SomeDependency` class and injects it into the callable. All callable types are supported: Controller methods (and constructor methods!), functions and Closures.\n\nAll injections are also recursive in nature, which means that the injected classes can also benefit from configuration-free dependency injection by type hinting injections in their respective constructor methods.\n\n### Middlewares\n\nMiddlewares are a way to run code before the actual route is dispatched. You can use middlewares to check if a user is authenticated, or if a user has the right permissions to access a route, etc. Middlewares are added to the router by using the `middleware` method, like so:\n\n```php\nuse Asko\\Router\\Router;\n\nclass SomeMiddleware\n{\n    public function handle(string $who): string\n    {\n        return \"intercepted, {$who}!\";\n    }\n}\n\n$router = new Router();\n\n$router-\u003eget(\n    path: '/hello/{who}', \n    callable: fn(string $who) =\u003e \"Hello, {$who}!\",\n    middlewares: [SomeMiddleware::class]\n});\n```\n\nWhen the above route is dispatched, the `SomeMiddleware` class will be instantiated and the `handle` method will be called. If the `handle` method returns anything other than `null`, the route will not be dispatched and the return value of the `handle` method will be returned instead.\n\nThe `handle` method of a middleware also fully supports dependency injection, and can make use of the same parameters as in the route itself.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faskonomm%2Frouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faskonomm%2Frouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faskonomm%2Frouter/lists"}