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 · 11 comments · May be fixed by #596
Open

Proposal of compact syntax for Flight::route #579

fadrian06 opened this issue Apr 24, 2024 · 11 comments · May be fixed by #596

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

@fadrian06 fadrian06 linked a pull request Jun 3, 2024 that will close this issue
@ampmonteiro
Copy link

ampmonteiro commented Jul 13, 2024

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

n0nag0n commented Jul 13, 2024

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

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

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. ;)

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

Successfully merging a pull request may close this issue.

3 participants