Welcome Guest, Not a member yet? Register   Sign In
Could #-attributes in PHP 8.1 be used for routes in the future?
#1

I have no clue what I am talking about so I am just dumping my thoughts here...
I have a fairly large project in CI4.2 which is a REST backend to an admin interface written as SPA JavaScript application. The Routes.php file is starting to get very long with segments like this to manage a single object in the system:

PHP Code:
// Routes for handling Users and Employees
$routes->options('users/(.+)''Options::index');
$routes->group('users', ['filter' => 'bearer-auth:user,empl'], function($routes) {
    $routes->get('certificates''Users::get_certificates');
    $routes->get('sessions''Users::get_sessions');
    $routes->get('badges''Users::get_badges');
    $routes->get('credits''Users::get_credits');
    $routes->get('credit_count''Users::get_credit_count');
    $routes->get('active_vouchers''Users::get_active_vouchers');
    $routes->get('showcert/(:segment)''Users::get_showcert/$1');
    $routes->get('mailcert/(:segment)''Users::get_mailcert/$1');
    $routes->get('uuid''Users::get_uuid');
    $routes->get('transactions''Users::get_transactions');
    $routes->post('credits''Users::post_credits');
    $routes->post('wipe_credit''Users::post_wipe_credit');
    $routes->post('clear_credits''Users::post_clear_credits');
    $routes->post('revoke_active''Users::post_revoke_active');
    $routes->post('merge''Users::post_merge');
    $routes->post('link''Users::post_link');
    $routes->post('password''Users::post_password');
    $routes->post('approve_emailval''Users::post_approve_emailval');
    $routes->post('approve_emailchange''Users::post_approve_emailchange');
    $routes->post('sync_badges''Users::post_sync_badges');
    $routes->post('issuecert''Users::post_issuecert');
    $routes->post('addcomment''Users::post_addcomment');
    $routes->post('new''Users::new');
    $routes->post('lmresync''Users::post_lmresync');
    $routes->delete('certificates/(:segment)''Users::delete_certificate/$1');
});
$routes->resource('users', ['controller' => 'Users''filter' => 'bearer-auth:user,empl']); 

Especially the filter settings are hord to remember the correct syntax. Also making sure that all possible routes are covered and maintained is getting harder and harder. Refactoring is also a nightmare.
So I thought: what if Attributes in the Controller itself could be used to describe the routes allowed in the Controller. Sort of lite auto-routes from before but with better maintainability. Example:

PHP Code:
<?php

#[RouteGroup('users')]
#[RouteFilter('bearer-auth:user,empl')]

class Users extends BaseController {

 
#[RouteMethod('get')]
 
public function certificates() {
 
// ...
 
}

 
#[RouteMethod('get')]
 #[RouteUri('showcert/(:segment)')]
 
public function showcert($id) {
 
// ...
 
}



As you can see I don't have a clue if this is possible or not but I just wanted to put my thoughts in writing and possibly start a discussion.
/Mattias
Reply
#2
Lightbulb 

maybe https://github.com/kenjis/ci4-attribute-routes helps
Reply
#3

https://forum.codeigniter.com/showthread.php?tid=81038 CodeIgniter4 Attribute Routes
Reply
#4

(09-21-2022, 08:22 AM)iRedds Wrote: https://forum.codeigniter.com/showthread.php?tid=81038 CodeIgniter4 Attribute Routes

Cool, thanks.
Reply
#5

Attribute route only runs in php version 8, while ci4 allows php version 7.
Reply
#6

(09-25-2022, 07:22 AM)titounnes Wrote: Attribute route only runs in php version 8, while ci4 allows php version 7.

Correct, but with 7.4 becoming unsupported in 2 months I wouldn't be surprised if support for PHP 7 will be dropped soon.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB