{"id":14987343,"url":"https://github.com/kenjis/ci4-attribute-routes","last_synced_at":"2025-04-12T00:01:54.189Z","repository":{"id":43652909,"uuid":"449157610","full_name":"kenjis/ci4-attribute-routes","owner":"kenjis","description":"CodeIgniter4 Attribute Routes. You can set Routes in Controllers as PHP8 Attributes.","archived":false,"fork":false,"pushed_at":"2023-02-12T08:30:49.000Z","size":90,"stargazers_count":25,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"1.x","last_synced_at":"2025-03-25T17:50:01.277Z","etag":null,"topics":["codeigniter","codeigniter4","routes","routing"],"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/kenjis.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":"2022-01-18T05:50:51.000Z","updated_at":"2025-03-11T12:57:45.000Z","dependencies_parsed_at":"2024-09-25T00:32:57.150Z","dependency_job_id":null,"html_url":"https://github.com/kenjis/ci4-attribute-routes","commit_stats":{"total_commits":59,"total_committers":1,"mean_commits":59.0,"dds":0.0,"last_synced_commit":"688b7db3a02f77d201844a72711f50bcbff6ab87"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenjis%2Fci4-attribute-routes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenjis%2Fci4-attribute-routes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenjis%2Fci4-attribute-routes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kenjis%2Fci4-attribute-routes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kenjis","download_url":"https://codeload.github.com/kenjis/ci4-attribute-routes/tar.gz/refs/heads/1.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248497805,"owners_count":21113984,"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":["codeigniter","codeigniter4","routes","routing"],"created_at":"2024-09-24T14:14:28.757Z","updated_at":"2025-04-12T00:01:54.137Z","avatar_url":"https://github.com/kenjis.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CodeIgniter4 Attribute Routes\n\nThis package generates a **Routes File** from the **Attribute Routes** in your **Controllers**.\n\n- You can set routes in your Controllers, and disable **Auto Routing**.\n- It generates a Routes File, so, there is no extra overhead at runtime.\n- The generated Routes File can be used on PHP 7.3 production servers.\n\n```php\nuse Kenjis\\CI4\\AttributeRoutes\\Route;\n\nclass SomeController extends BaseController\n{\n    #[Route('path', methods: ['get'])]\n    public function index()\n    {\n        ...\n    }\n}\n```\n\n## Requirements\n\n- CodeIgniter 4.3.1 or later\n- Composer\n- PHP 8.0 or later\n\n## Installation\n\n```sh-session\n$ composer require kenjis/ci4-attribute-routes\n```\n\n## Configuration\n\n1. Add the following code to the bottom of your `app/Config/Routes.php` file:\n```php\n/*\n * Attribute Routes\n *\n * To update the route file, run the following command:\n * $ php spark route:update\n *\n * @see https://github.com/kenjis/ci4-attribute-routes\n */\nif (file_exists(APPPATH . 'Config/RoutesFromAttribute.php')) {\n    require APPPATH . 'Config/RoutesFromAttribute.php';\n}\n```\n\n2. Disable auto routing and enable route priority:\n```diff\n--- a/app/Config/Routes.php\n+++ b/app/Config/Routes.php\n@@ -22,7 +22,8 @@ $routes-\u003esetDefaultController('Home');\n $routes-\u003esetDefaultMethod('index');\n $routes-\u003esetTranslateURIDashes(false);\n $routes-\u003eset404Override();\n-$routes-\u003esetAutoRoute(true);\n+$routes-\u003esetAutoRoute(false);\n+$routes-\u003esetPrioritize();\n```\n\nThis is optional, but strongly recommended.\n\n## Quick Start\n\n### 1. Add Attribute Routes to your Controllers\n\nAdd `#[Route()]` attributes to your Controller methods.\n\n```php\n\u003c?php\nnamespace App\\Controllers;\n\nuse Kenjis\\CI4\\AttributeRoutes\\Route;\n\nclass News extends BaseController\n{\n    #[Route('news', methods: ['get'])]\n    public function index()\n    {\n        ...\n    }\n}\n```\n\n### 2. Update Routes File\n\n```sh-session\n$ php spark route:update\n```\n\n`APPPATH/Config/RoutesFromAttribute.php` is generated.\n\nCheck your routes with the `php spark routes` command.\n\n## Route Attributes\n\n### Route\n\n```php\n#[Route('news', methods: ['get'])]\n```\n```php\n#[Route('news/create', methods: ['get', 'post'])]\n```\n```php\n#[Route('news/(:segment)', methods: ['get'], options: ['priority' =\u003e 1])]\n```\n\n### RouteGroup\n\n```php\nuse Kenjis\\CI4\\AttributeRoutes\\RouteGroup;\n\n#[RouteGroup('', options: ['filter' =\u003e 'auth'])]\nclass GroupController extends BaseController\n{\n    #[Route('group/a', methods: ['get'])]\n    public function getA(): void\n    {\n        ...\n    }\n    ...\n}\n```\n\n### RouteResource\n\n```php\nuse Kenjis\\CI4\\AttributeRoutes\\RouteResource;\n\n#[RouteResource('photos', options: ['websafe' =\u003e 1])]\nclass ResourceController extends ResourceController\n{\n    ...\n}\n```\n\n### RoutePresenter\n\n```php\nuse Kenjis\\CI4\\AttributeRoutes\\RoutePresenter;\n\n#[RoutePresenter('presenter')]\nclass PresenterController extends ResourcePresenter\n{\n    ...\n}\n```\n\n## Trouble Shooting\n\n### No routes in the generated routes file\n\nYou must import the attribute classes in your controllers.\n\nE.g.:\n```php\nuse Kenjis\\CI4\\AttributeRoutes\\Route;\n...\n    #[Route('news', methods: ['get'])]\n    public function index()\n```\n\n### Can't be routed correctly, or 404 error occurs\n\nShow your routes with the `php spark routes` command, and check the order of the routes.\nThe first matched route is the one that is executed.\nThe placeholders like `(.*)` or `([^/]+)` takes any characters or segment. So you have to move the routes like that to the bottom.\n\nIn one controller, you can move the methods having such routes to the bottom.\n\nOr set the priority of the routes with `options`:\n```php\n#[Route('news/(:segment)', methods: ['get'], options: ['priority' =\u003e 1])]\n```\nZero is the default priority, and the higher the number specified in the `priority` option, the lower route priority in the processing queue.\n\n## For Development\n\n### Installation\n\n    composer install\n\n### Available Commands\n\n    composer test              // Run unit test\n    composer tests             // Test and quality checks\n    composer cs-fix            // Fix the coding style\n    composer sa                // Run static analysys tools\n    composer run-script --list // List all commands\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenjis%2Fci4-attribute-routes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkenjis%2Fci4-attribute-routes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkenjis%2Fci4-attribute-routes/lists"}