PHPackages                             advancedynamic/codeigniter-attributeroutes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. advancedynamic/codeigniter-attributeroutes

ActiveLibrary[Framework](/categories/framework)

advancedynamic/codeigniter-attributeroutes
==========================================

CodeIgniter4 Attribute Routes module

v1.0.2(1y ago)06MITPHPPHP ^8.1

Since Feb 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/advancedynamic/codeigniter-attribute-routes)[ Packagist](https://packagist.org/packages/advancedynamic/codeigniter-attributeroutes)[ RSS](/packages/advancedynamic-codeigniter-attributeroutes/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

Custom Routing Library for CodeIgniter 4
========================================

[](#custom-routing-library-for-codeigniter-4)

This library enhances the routing system of CodeIgniter 4 by introducing PHP 8 Attributes. It allows defining routes directly in controller methods, thereby simplifying the routing process and reducing redundancy. The library also supports multiple controller namespaces and integrates middleware easily with routes.

Features
--------

[](#features)

- Define routes using PHP 8 Attributes.
- Support for GET, POST, PUT, DELETE, PATCH, and OPTIONS methods.
- Regular expression patterns in routes.
- Filter integration with routes.
- Multiple controller namespaces.

Installation
------------

[](#installation)

1. **Install via Composer**

    Add the library to your CodeIgniter 4 project using Composer:

    ```
    composer require advancedynamic/codeigniter-attributeroutes
    ```
2. **Update the Bootstrap File**

    ```
    use Advancedynamic\Attributeroutes\Router\CustomRouter;
    use Config\Services;

    $routes = Services::routes();
    $customRouter = new CustomRouter($routes, Services::request(), ['App\\Controllers']);
    $customRouter->initialize();

    Services::injectMock('router', $customRouter);
    ```

    Adjust the namespaces in the array as per your project structure.

Usage
-----

[](#usage)

**Defining Routes**

Use attributes to define routes directly in the controller methods:

```
namespace App\Controllers;

use Advancedynamic\Codeigniter\Attributeroutes\Attributes\GetRoute;
use Advancedynamic\Codeigniter\Attributeroutes\Attributes\PostRoute;

class MyController {
    #[GetRoute('/', ['filter'=> 'MyFilter'])]
    public function index() {
        return "GET method with for index";
    }

    #[GetRoute('/test-get/(\d+)')]
    public function testGetMethod($id) {
        return "GET method with ID: $id";
    }

    #[PostRoute('/test-post')]
    public function testPostMethod() {
        return "POST method";
    }

}
```

Usage with different namespace
------------------------------

[](#usage-with-different-namespace)

1. **Update the Bootstrap File**

    **Modify your app/Config/Routes.php to use the custom router:**

    ```
    use Advancedynamic\Codeigniter\Attributeroutes\Router\CustomRouter;
    use Config\Services;

    $routes = Services::routes();
    $routes->setDefaultNamespace('');

    // Wildcard namespace: the '*' will be replaced by each module folder under ROOTPATH/modules
    $customRouter = new CustomRouter($routes, Services::request(), ['App\\Controllers', 'Modules\\*\\Controllers']);
    $customRouter->initialize();

    Services::injectMock('router', $customRouter);
    ```

    **Modify your app/Config/Autoload.php to use the custom router, add this code to the class**

    ```
    public function __construct() {
        parent::__construct();

        foreach(glob(ROOTPATH . 'modules/*', GLOB_ONLYDIR) as $item_dir) {
            $explode = explode(DIRECTORY_SEPARATOR, $item_dir);
            if (file_exists($item_dir)) {
                $this->psr4['Modules\\'.end($explode)] = ROOTPATH . 'modules/'.end($explode);
            }
        }
    }
    ```

    To use the class you can build a class in the following folder

    ```
    └── Codeigniter project/
        └── Modules/
            ├── Controllers/
            │   └── Dashboard.php
            ├── Views
            ├── Config
            └── Models

    ```

    ```
