PHPackages                             akara/ci4\_route\_generator - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. akara/ci4\_route\_generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

akara/ci4\_route\_generator
===========================

Codeigniter 4 Route Generator

1.0.3(1y ago)011MITPHPPHP ^8.0

Since Feb 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Nxx1/ci4_route_generator)[ Packagist](https://packagist.org/packages/akara/ci4_route_generator)[ Docs](https://github.com/nxx1/ci4_route_generator)[ RSS](/packages/akara-ci4-route-generator/feed)WikiDiscussions main Synced 1mo ago

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

Codeigniter 4 Route Generator
=============================

[](#codeigniter-4-route-generator)

The **Codeigniter 4 Route Generator** is a PHP package designed to automate the generation of routes for CodeIgniter 4 applications. It scans your controller classes, extracts route information from custom attributes, and automatically generates route definitions in a specified routes file.

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

[](#installation)

To install the package via Composer, run the following command:

```
composer require akara/ci4-route-generator
```

Usage
-----

[](#usage)

### 1. Define Routes Using Attributes

[](#1-define-routes-using-attributes)

In your controller classes, use the `RoutePath` attribute to define routes. For example:

```
namespace App\Controllers;

use Akara\RouteGenerator\RoutePath;
use CodeIgniter\Controller;

class UserController extends Controller
{
    #[RoutePath('/users', 'get')]
    public function index()
    {
        // Your logic here
    }

    #[RoutePath('/users/{id}', 'get', ['auth'])]
    public function show($id)
    {
        // Your logic here
    }
}
```

### 2. Run the Route Generator

[](#2-run-the-route-generator)

To generate routes, run the following command in your terminal:

```
php spark make:route
```

This command will scan all controller classes within the specified namespaces, extract route information, and append the generated routes to the `Config/RoutesCustom.php` file.

### 3. Include Generated Routes

[](#3-include-generated-routes)

Add the following line to `app/config/Routes.php` to include the generated routes:

```
namespace Config;

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */

include_once('RoutesCustom.php');
```

With these steps, your routes will be automatically generated and added to your application, simplifying the management of routes.

Code Documentation
------------------

[](#code-documentation)

### `GenerateRoutes` Command

[](#generateroutes-command)

The `GenerateRoutes` command is responsible for initiating the route generation process.

#### Properties

[](#properties)

- **`$group`**: The command group (e.g., `Generators`).
- **`$name`**: The command name (e.g., `make:route`).
- **`$description`**: A brief description of the command.

#### Methods

[](#methods)

- **`run(array $params)`**: Executes the route generation process. It retrieves all namespaces from the autoloader, initializes the `CustomRouter`, and writes the generated routes to the routes file.

### `CustomRouter` Class

[](#customrouter-class)

The `CustomRouter` class extends the CodeIgniter `Router` and handles the generation of routes based on the scanned controller classes.

#### Properties

[](#properties-1)

- **`$controllerNamespaces`**: An array of namespaces to scan for controller classes.

#### Methods

[](#methods-1)

- **`__construct(RouteCollectionInterface $routes, RequestInterface $request = null, array $controllerNamespace = [])`**: Initializes the `CustomRouter` with the provided routes, request, and namespaces.
- **`initialize()`**: Scans the specified namespaces for controller classes and generates routes based on the `RoutePath` attributes.
- **`addRoutesToFile(array $routes)`**: Writes the generated routes to the `RoutesCustom.php` file.

### `RoutePath` Attribute

[](#routepath-attribute)

The `RoutePath` attribute is used to define route information in controller methods.

#### Properties

[](#properties-2)

- **`$path`**: The route path.
- **`$method`**: The HTTP method (e.g., `get`, `post`).
- **`$filters`**: An array of filters to apply to the route.

### `RouteScanner` Class

[](#routescanner-class)

The `RouteScanner` class is responsible for scanning controller classes and extracting route information from the `RoutePath` attributes.

#### Methods

[](#methods-2)

- **`scan(string $controllerNamespace)`**: Scans the specified namespace for controller classes and extracts route information.
- **`getControllerClasses($namespace)`**: Retrieves all controller classes within the specified namespace.
- **`processAttributes($method)`**: Processes the `RoutePath` attributes of a controller method and returns the route information.
- **`convertNamespaceToPath($namespace)`**: Converts a namespace to a filesystem path.

Example
-------

[](#example)

### Controller with `RoutePath` Attributes

[](#controller-with-routepath-attributes)

```
namespace App\Controllers;

use Akara\RouteGenerator\RoutePath;
use App\Filters\SessionFilters;
use CodeIgniter\Controller;

class UserController extends Controller
{
    #[RoutePath('/users', 'GET')]
    public function index()
    {
        // Your logic here
    }

    #[RoutePath('/users/{id}', 'GET', [SessionFilters::class])]
    public function show($id)
    {
        // Your logic here
    }
}
```

### Generated Routes

[](#generated-routes)

After running the `make:route` command, the following routes will be added to `Config/RoutesCustom.php`:

```
