Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal of compact syntax for Flight::route #579

Open
fadrian06 opened this issue Apr 24, 2024 · 7 comments
Open

Proposal of compact syntax for Flight::route #579

fadrian06 opened this issue Apr 24, 2024 · 7 comments

Comments

@fadrian06
Copy link
Contributor

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
Copy link
Contributor Author

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

@n0nag0n
Copy link
Collaborator

n0nag0n commented Apr 25, 2024

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Collaborator

n0nag0n commented Apr 28, 2024

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

@fadrian06
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants