All Projects → timacdonald → Has Parameters

timacdonald / Has Parameters

Licence: mit
A trait that allows you to pass arguments to Laravel middleware in a more PHP'ish way.

Projects that are alternatives of or similar to Has Parameters

Laravel Guided Image
Simplified and ready image manipulation for Laravel through intervention image.
Stars: ✭ 32 (-78.52%)
Mutual labels:  hacktoberfest, laravel, trait
Vuex Rest Api
A utility to simplify the use of REST APIs with Vuex
Stars: ✭ 365 (+144.97%)
Mutual labels:  hacktoberfest, middleware, helper
Onramp
Easing the onramp for new or non-PHP developers to become Laravel devs.
Stars: ✭ 123 (-17.45%)
Mutual labels:  hacktoberfest, laravel
Voyager
Voyager - The Missing Laravel Admin
Stars: ✭ 10,801 (+7148.99%)
Mutual labels:  hacktoberfest, laravel
Health
Laravel Health Panel
Stars: ✭ 1,774 (+1090.6%)
Mutual labels:  hacktoberfest, laravel
Goodwork
Self hosted project management and collaboration tool powered by TALL stack
Stars: ✭ 1,730 (+1061.07%)
Mutual labels:  hacktoberfest, laravel
Facebook
📨 Facebook Notifications Channel for Laravel
Stars: ✭ 120 (-19.46%)
Mutual labels:  hacktoberfest, laravel
Oc Mall Plugin
🏪 E-commerce solution for October CMS
Stars: ✭ 128 (-14.09%)
Mutual labels:  hacktoberfest, laravel
Laracom
Laravel FREE E-Commerce Software
Stars: ✭ 1,570 (+953.69%)
Mutual labels:  hacktoberfest, laravel
Laravel Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.
Stars: ✭ 136 (-8.72%)
Mutual labels:  middleware, laravel
Simple Qrcode
An easy-to-use PHP QrCode generator with first-party support for Laravel.
Stars: ✭ 1,923 (+1190.6%)
Mutual labels:  hacktoberfest, laravel
Jigsaw
Simple static sites with Laravel’s Blade.
Stars: ✭ 1,823 (+1123.49%)
Mutual labels:  hacktoberfest, laravel
Collector Intellij
A PhpStorm plugin for refactoring to collections
Stars: ✭ 114 (-23.49%)
Mutual labels:  hacktoberfest, laravel
Laravel Api Boilerplate
A Boilerplate Project For Laravel API's (NOT MAINTAINED)
Stars: ✭ 113 (-24.16%)
Mutual labels:  hacktoberfest, laravel
Laravel Mail Editor
MailEclipse ⚡ Laravel Mailable Editor!
Stars: ✭ 1,714 (+1050.34%)
Mutual labels:  hacktoberfest, laravel
Laravel Translations Loader
Webpack loader to import Laravel translation files (PHP or JSON) into your JS bundle as JSON.
Stars: ✭ 109 (-26.85%)
Mutual labels:  hacktoberfest, laravel
L5 Very Basic Auth
Stateless HTTP basic auth for Laravel without the need for a database.
Stars: ✭ 127 (-14.77%)
Mutual labels:  middleware, laravel
Pipedrive
Complete Pipedrive API client for PHP
Stars: ✭ 138 (-7.38%)
Mutual labels:  hacktoberfest, laravel
Larasupport
📦 Adds Laravel Packages Support to Lumen and Vendor Publish Artisan Command.
Stars: ✭ 104 (-30.2%)
Mutual labels:  hacktoberfest, laravel
Collect
A Collections-only split from Laravel's Illuminate Support
Stars: ✭ 1,433 (+861.74%)
Mutual labels:  hacktoberfest, laravel

Has Parameters: a Laravel package by Tim MacDonald

Has Parameters

CI codecov Mutation testing Type coverage

A trait for Laravel middleware that allows you to pass arguments in a more PHP'ish way, including as a key => value pair for named parameters, and as a list for variadic 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.

Read more about the why in my blog post Rethinking Laravel's middleware argument API

Version support

  • PHP: 7.1, 7.2, 7.3, 7.4
  • Laravel: 5.5, 5.6, 5.7, 5.8, 6.0, 7.0, 8.0

Installation

You can install using composer from Packagist.

$ composer require timacdonald/has-parameters

Basic usage

To get started with an example, I'm going to use a stripped back version of Laravel's ThrottleRequests. First up, add the HasParameters trait to your middleware.

<?php

class ThrottleRequests
{
    use HasParameters;

    public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')
    {
        //
    }
}

You can now pass arguments to this middleware using the static with() method, using the parameter name as the key.

<?php

Route::stuff()
    ->middleware([
        ThrottleRequests::with([
            'maxAttempts' => 120,
        ]),
    ]);

You'll notice at first this is a little more verbose, but I think you'll enjoy the complete feature set after reading these docs and taking it for a spin.

Middleware::with()

The static with() method allows you to easily see which values represent what when declaring your middleware, instead of just declaring a comma seperate list of values. The order of the keys does not matter. The trait will pair up the keys to the parameter names in the handle() method.

<?php

// before...
Route::stuff()
    ->middleware([
        'throttle:10,1' // what does 10 or 1 stand for here?
    ]);

// after...
Route::stuff()
    ->middleware([
        ThrottleRequests::with([
            'decayMinutes' => 1,
            'maxAttempts' => 10,
        ]),
    ]);

Skipping parameters

If any parameters in the handle method have a default value, you do not need to pass them through - unless you are changing their value. As an example, if you'd like to only specify a prefix for the ThrottleRequests middleware, but keep the $decayMinutes and $maxAttempts as their default values, you can do the following...

<?php

Route::stuff()
    ->middleware([
        ThrottleRequests::with([
            'prefix' => 'admins',
        ]),
    ]);

As we saw previously in the handle method, the default values of $decayMinutes is 1 and $maxAttempts is 60. The middleware will receive those values for those parameters, but will now receive "admins" for the $prefix.

Arrays for variadic parameters

When your middleware ends in a variadic paramater, you can pass an array of values for the variadic parameter key. Take a look at the following handle() method.

<?php

public function handle(Request $request, Closure $next, string $ability, string ...$models)

Here is how we can pass a list of values to the variadic $models parameter...

<?php

Route::stuff()
    ->middleware([
        Authorize::with([
            'ability' => PostVideoPolicy::UPDATE,
            'models' => [Post::class, Video::class],
        ]),
    ]);

Validation

These validations occur whenever the routes file is loaded or compiled, not just when you hit a route that contains the declaration.

Unexpected parameter

The trait validates that you do not declare any keys that do not exist as parameter variables in the handle() method. This helps make sure you don't mis-type a parameter name.

Required parameters

Another validation that occurs is checking to make sure all required parameters (those without default values) have been provided.

Middleware::in()

The static in() method very much reflects and works the same as the existing concatination API. It accepts a list of values, i.e. a non-associative array. You should use this method if your handle() method is a single variadic parameter, i.e. expecting a single list of values, as shown in the following middleware handle method... .

<?php

public function handle(Request $request, Closure $next, string ...$states)
{
    //
}

You can pass through a list of "states" to the middleware like so...

<?php

Route::stuff()
    ->middleware([
        EnsurePostState::in([PostState::DRAFT, PostState::UNDER_REVIEW]),
    ]);

Validation

Required parameters

Just like the with() method, the in() method will validate that you have passed enough values through to cover all the required parameters. Because variadic parameters do not require any values to be passed through, you only really rub up against this when you should probably be using the with() method.

Value transformation

You should keep in mind that everything will still be cast to a string. Although you are passing in, for example, integers, the middleware itself will always receive a string. This is how Laravel works under-the-hood to implement route caching.

One thing to note is the false is actually cast to the string "0" to keep some consistency with casting true to a string, which results in the string "1".

Developing and testing

Although this package requires "PHP": "^7.1", in order to install and develop locally, you should be running a recent version of PHP to ensure compatibility with the development tools.

Credits

And a special (vegi) thanks to Caneco for the logo ✨

Thanksware

You are free to use this package, but I ask that you reach out to someone (not me) who has previously, or is currently, maintaining or contributing to an open source library you are using in your project and thank them for their work. Consider your entire tech stack: packages, frameworks, languages, databases, operating systems, frontend, backend, etc.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].