Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kenjis/ci4-attribute-routes
CodeIgniter4 Attribute Routes. You can set Routes in Controllers as PHP8 Attributes.
https://github.com/kenjis/ci4-attribute-routes
codeigniter codeigniter4 routes routing
Last synced: 3 months ago
JSON representation
CodeIgniter4 Attribute Routes. You can set Routes in Controllers as PHP8 Attributes.
- Host: GitHub
- URL: https://github.com/kenjis/ci4-attribute-routes
- Owner: kenjis
- License: mit
- Created: 2022-01-18T05:50:51.000Z (about 3 years ago)
- Default Branch: 1.x
- Last Pushed: 2023-02-12T08:30:49.000Z (almost 2 years ago)
- Last Synced: 2024-10-01T00:41:22.131Z (4 months ago)
- Topics: codeigniter, codeigniter4, routes, routing
- Language: PHP
- Homepage:
- Size: 87.9 KB
- Stars: 21
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.```php
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
```sh-session
$ composer require kenjis/ci4-attribute-routes
```## Configuration
1. Add the following code to the bottom of your `app/Config/Routes.php` file:
```php
/*
* 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:
```diff
--- 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
1])]
```### RouteGroup
```php
use Kenjis\CI4\AttributeRoutes\RouteGroup;#[RouteGroup('', options: ['filter' => 'auth'])]
class GroupController extends BaseController
{
#[Route('group/a', methods: ['get'])]
public function getA(): void
{
...
}
...
}
```### RouteResource
```php
use Kenjis\CI4\AttributeRoutes\RouteResource;#[RouteResource('photos', options: ['websafe' => 1])]
class ResourceController extends ResourceController
{
...
}
```### RoutePresenter
```php
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.:
```php
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`:
```php
#[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