kenjis/ci4-attribute-routes

CodeIgniter4 Attribute Routes module

v0.3.1 2023-02-12 08:20 UTC

This package is auto-updated.

Last update: 2024-11-12 12:15:10 UTC


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

  1. 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';
}
  1. 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