PHPackages                             kenjis/ci4-attribute-routes - 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. kenjis/ci4-attribute-routes

ActiveLibrary[Framework](/categories/framework)

kenjis/ci4-attribute-routes
===========================

CodeIgniter4 Attribute Routes module

v0.3.1(3y ago)275725MITPHPPHP ^8.0

Since Jan 27Pushed 3y ago3 watchersCompare

[ Source](https://github.com/kenjis/ci4-attribute-routes)[ Packagist](https://packagist.org/packages/kenjis/ci4-attribute-routes)[ Docs](https://github.com/kenjis/ci4-attribute-routes)[ RSS](/packages/kenjis-ci4-attribute-routes/feed)WikiDiscussions 1.x Synced today

READMEChangelog (4)Dependencies (12)Versions (5)Used By (0)

CodeIgniter4 Attribute Routes
=============================

[](#codeigniter4-attribute-routes)

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
------------

[](#requirements)

- CodeIgniter 4.3.1 or later
- Composer
- PHP 8.0 or later

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

[](#installation)

```
$ composer require kenjis/ci4-attribute-routes
```

Configuration
-------------

[](#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';
}
```

2. 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
-----------

[](#quick-start)

### 1. Add Attribute Routes to your Controllers

[](#1-add-attribute-routes-to-your-controllers)

Add `#[Route()]` attributes to your Controller methods.

```
