skipperbent / simple-php-router

Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
631 stars 116 forks source link

Passing parameters to middlewares #704

Open michaelthedev opened 6 months ago

michaelthedev commented 6 months ago

Hi I've looked through the documentation and other issues raised here and I can't find any working way to pass parameters to middleware

I'm looking to achieve something like this

Router::group( ['middleware' => ['limiter,1,2'] ], function () {});

or this

Router::post('/path-here', [Controller::class, 'method'])
    ->addMiddleware(Limiter::class, 'paramter1', 'parameter2');

// or 
Router::post('/path-here', [Controller::class, 'method'])
    ->addMiddleware(Limiter::class, ['paramter1', 'parameter2']);

I'm available to assist on this if needed. Thanks and Happy New Year

skipperbent commented 6 months ago

Hi. It doesn't have a wrapper like that for the middlewares at the moment unfortunately. If you need to pass variables you will have to add a function and pass them on the request object or create alternatively create middlewares and make them extend a base one.

request()->user = User::find(1);

Happy new year!