leafsphp / router

🏚 router module for leaf PHP
https://leafphp.dev/modules/router/
5 stars 5 forks source link

How to add middleware in this route app()->resource('/posts', 'PostsController'); #17

Open fitdev-github opened 7 months ago

fitdev-github commented 7 months ago

I tried to do this app()->resource('/posts', ['middleware' => 'auth','PostsController']);

but I got error TypeError: Leaf\Router::resource(): Argument leafsphp/leaf#2 ($controller) must be of type string, array given

VT-nkumar commented 7 months ago

As far as coding, resources don't support middleware. You can try this

$app->group('/', ['middleware' => 'auth', function () use ($app) {
  app()->resource('/posts', 'PostsController'); 
}]);

or

$app->group('/posts', ['middleware' => 'auth', function () use ($app) {
  app()->resource('/','PostsController'); 
}]);
fadrian06 commented 5 months ago

I think that at a low level there could be an adapter that transforms the requested and unsupported syntax, to the supported syntax

fadrian06 commented 5 months ago

Some adapter that transform this:

app()->resource('/posts', ['middleware'=> 'auth', 'PostController']);

to this:

app()->group('/posts', ['middleware' => 'auth', function () {
  app()->resource('/', 'PostController');
}]);