flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.64k stars 408 forks source link

Proposal of compact syntax for Flight::route #579

Closed fadrian06 closed 1 month ago

fadrian06 commented 7 months ago

This is an alternative of Resource controllers, for people like me that name routes in spanish but controllers in english (for cohesion)

Flight::route(
  '/users',
  UsersController::class,
  [
    'GET /' => 'list',
    'POST /' => 'handleRegister',
    // ...rest of definitions
  ]
);
fadrian06 commented 7 months ago

Technically it seems like Flight::group, but it's for one controller

n0nag0n commented 7 months ago

I like where you're going with this. I'm not sure if an array is the best way to assign the methods, or if those methods should be automatically assumed.....or maybe both? If you assign them it will follow how you assign them, but if you don't it will assume a standard. And maybe you could override just one of the methods if you wanted?

Just thinking out loud.

fadrian06 commented 7 months ago

If you only assign 3 methods with their respective controller methods, the rest could be assumed but only if those methods are defined in the controller

fadrian06 commented 7 months ago

So suppose I define to /id/editar in Spanish, the edit method and then not finding that you have defined a PUT|PATCH /id/edit I would add an /id/edit to the edit method, but only if it exists and has not been defined

fadrian06 commented 7 months ago

I think the use case is pretty good and the API is pretty easy to master.

You have an api in Spanish, and you only need the GET / and the POST / mapped to listAll and handleSave

Flight will see that there is no POST / definition to the store method, but since it is not defined it does not add it

Using a resource controller would be as simple as

Flight::route('/users', UserController::class);
// UserController.php
class UserController {
  function __construct(MyDependency $dep) {}

  function index(): void {}
  function show(string $id): void {}
  function create():  void {}
  function store(): void {}
  function edit(string $id): void {}
  function update(string $id): void {}
  function delete(string $id): void {}
}
n0nag0n commented 7 months ago

I can get behind that. If you just define a controller then it will add those 7 routes by default.

fadrian06 commented 6 months ago

Here is another example of the requested behavior

Flight::route('/posts', PostController::class);
class PostController {
  static function index(): void {}
  static function show(string $id): void {}
}

Flight will define the following routes:

[
  'GET /' => 'PostController::index',
  'GET /@id' => 'PostController::show'
]
ampmonteiro commented 4 months ago

Hi, maybe i am late to the party, however would be better to use

Flight::resource('/posts', PostController::class);

Like Laravel and CI4 does. ;)

n0nag0n commented 4 months ago

You may be late, but just in time 😊. Still haven’t made any formal decisions about what to call it. Resource definitely makes sense and is more explicit than just “knowing” that you could assign resources as another parameter. Still thinking about it 🤔

fadrian06 commented 4 months ago

Hi, maybe i am late to the party, however would be better to use

Flight::resource('/posts', PostController::class);

Like Laravel and CI4 does. ;)

yeah, but what if you don't want to use the naming conventions for controller methods (index, store, etc...) the idea is customize names in route mapping

ampmonteiro commented 4 months ago

Hi @fadrian06 ,

above you only show the case for traditional CRUD ( with all verbs),

Not the solution for customization.

For example, if you dont implement the edit or show CRUD, you can add a exception like Laravel or CI4 do.

CI4 :

routes->resource('photos', ['only' => ['index', 'update', 'delete']]);

// Or

$routes->resource('photos', ['except' => 'new, edit']);

the customs routes should be created as normal.

Nowadays for flexible router, if you are using a class (for controller) and PHP 8+, Any router library or Framework have to implement options to use PHP attributes for routes definitions and even to define the path of template / view. With this you dont have to manage files where the routes are defined ( this is way symfony works) and it is all in the class / controller.

For simple projects or Web API, this lib does what is necessary, and above solution also would be enough for your case. ;)