kenjis / ci4-attribute-routes
CodeIgniter4 Attribute Routes module
Requires
- php: ^8.0
Requires (Dev)
- codeigniter4/codeigniter4: dev-develop
- codeigniter4/devkit: ^1.0
- doctrine/coding-standard: ^9.0
- icanhazstring/composer-unused: ^0.8.1
- kenjis/phpunit-helper: ^1.1.2
- phpmd/phpmd: ^2.11
- phpmetrics/phpmetrics: ^2.7
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.13
- rector/rector: 0.15.13
- squizlabs/php_codesniffer: ^3.6
- vimeo/psalm: ^4.18
Suggests
- ext-fileinfo: Improves mime type detection for files
README
This package generates a Routes File from the Attribute Routes in your Controllers.
- You can set routes in your Controllers, and disable Auto Routing.
- It generates a Routes File, so, there is no extra overhead at runtime.
- The generated Routes File can be used on PHP 7.3 production servers.
use Kenjis\CI4\AttributeRoutes\Route; class SomeController extends BaseController { #[Route('path', methods: ['get'])] public function index() { ... } }
Requirements
- CodeIgniter 4.3.1 or later
- Composer
- PHP 8.0 or later
Installation
$ composer require kenjis/ci4-attribute-routes
Configuration
- Add the following code to the bottom of your
app/Config/Routes.php
file:
/* * Attribute Routes * * To update the route file, run the following command: * $ php spark route:update * * @see https://github.com/kenjis/ci4-attribute-routes */ if (file_exists(APPPATH . 'Config/RoutesFromAttribute.php')) { require APPPATH . 'Config/RoutesFromAttribute.php'; }
- Disable auto routing and enable route priority:
--- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -22,7 +22,8 @@ $routes->setDefaultController('Home'); $routes->setDefaultMethod('index'); $routes->setTranslateURIDashes(false); $routes->set404Override(); -$routes->setAutoRoute(true); +$routes->setAutoRoute(false); +$routes->setPrioritize();
This is optional, but strongly recommended.
Quick Start
1. Add Attribute Routes to your Controllers
Add #[Route()]
attributes to your Controller methods.
<?php namespace App\Controllers; use Kenjis\CI4\AttributeRoutes\Route; class News extends BaseController { #[Route('news', methods: ['get'])] public function index() { ... } }
2. Update Routes File
$ php spark route:update
APPPATH/Config/RoutesFromAttribute.php
is generated.
Check your routes with the php spark routes
command.
Route Attributes
Route
#[Route('news', methods: ['get'])]
#[Route('news/create', methods: ['get', 'post'])]
#[Route('news/(:segment)', methods: ['get'], options: ['priority' => 1])]
RouteGroup
use Kenjis\CI4\AttributeRoutes\RouteGroup; #[RouteGroup('', options: ['filter' => 'auth'])] class GroupController extends BaseController { #[Route('group/a', methods: ['get'])] public function getA(): void { ... } ... }
RouteResource
use Kenjis\CI4\AttributeRoutes\RouteResource; #[RouteResource('photos', options: ['websafe' => 1])] class ResourceController extends ResourceController { ... }
RoutePresenter
use Kenjis\CI4\AttributeRoutes\RoutePresenter; #[RoutePresenter('presenter')] class PresenterController extends ResourcePresenter { ... }
Trouble Shooting
No routes in the generated routes file
You must import the attribute classes in your controllers.
E.g.:
use Kenjis\CI4\AttributeRoutes\Route; ... #[Route('news', methods: ['get'])] public function index()
Can't be routed correctly, or 404 error occurs
Show your routes with the php spark routes
command, and check the order of the routes.
The first matched route is the one that is executed.
The placeholders like (.*)
or ([^/]+)
takes any characters or segment. So you have to move the routes like that to the bottom.
In one controller, you can move the methods having such routes to the bottom.
Or set the priority of the routes with options
:
#[Route('news/(:segment)', methods: ['get'], options: ['priority' => 1])]
Zero is the default priority, and the higher the number specified in the priority
option, the lower route priority in the processing queue.
For Development
Installation
composer install
Available Commands
composer test // Run unit test
composer tests // Test and quality checks
composer cs-fix // Fix the coding style
composer sa // Run static analysys tools
composer run-script --list // List all commands