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()
{
...
}
}
- CodeIgniter 4.3.1 or later
- Composer
- PHP 8.0 or later
$ composer require kenjis/ci4-attribute-routes
- 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.
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()
{
...
}
}
$ php spark route:update
APPPATH/Config/RoutesFromAttribute.php
is generated.
Check your routes with the php spark routes
command.
#[Route('news', methods: ['get'])]
#[Route('news/create', methods: ['get', 'post'])]
#[Route('news/(:segment)', methods: ['get'], options: ['priority' => 1])]
use Kenjis\CI4\AttributeRoutes\RouteGroup;
#[RouteGroup('', options: ['filter' => 'auth'])]
class GroupController extends BaseController
{
#[Route('group/a', methods: ['get'])]
public function getA(): void
{
...
}
...
}
use Kenjis\CI4\AttributeRoutes\RouteResource;
#[RouteResource('photos', options: ['websafe' => 1])]
class ResourceController extends ResourceController
{
...
}
use Kenjis\CI4\AttributeRoutes\RoutePresenter;
#[RoutePresenter('presenter')]
class PresenterController extends ResourcePresenter
{
...
}
You must import the attribute classes in your controllers.
E.g.:
use Kenjis\CI4\AttributeRoutes\Route;
...
#[Route('news', methods: ['get'])]
public function index()
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.
composer install
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