Has Parameters is a Laravel package by Tim MacDonald to “pass arguments [to middleware] in a more PHP’ish way.”
Here’s an example of the primary usage as found in the project’s readme:
First, the following is a simplified example of what Laravel’s ThrottleRequests
middleware might look like:
1class ThrottleRequests2{3 use HasParameters;4 5 public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')6 {7 //8 }9}
Then you could configure the middleware in a route like so:
1Route::stuff()2 ->middleware([3 ThrottleRequests::with([4 'maxAttempts' => 120,5 ]),6 ]);
As you can see from the example, this package’s with()
method found in the HasParameters
trait will take care of working with parameters that have default values.
The readme outlines additional benefits of using this package with your app’s middleware:
Has Parameters improves static analysis / IDE support, allows you to specify arguments by referencing the parameter name, enables skipping optional parameters (which fallback to their default value), and adds some validation, so you don’t forget any required parameters by accident.
0 comments:
Post a Comment
Thanks