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

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->group('users', ['filter' => 'bearer-auth:user,empl'], function($routes) {
$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:


class Users extends BaseController {

public function certificates() {
// ...

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.

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

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

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

Cool, thanks.

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

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

Theme © iAndrew 2016 - Forum software by © MyBB