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

[10.x] Alternative to the stringy middleware API #46219

Closed
wants to merge 7 commits into from

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Feb 22, 2023

This is simplified and framework first implementation of my HasParameters package.

Removes the need to use string concatenation when specifying middleware. No need for middleware classes to extend a base class or use a trait. The framework just handles things.

Note This is just a wrapper around the stringy API. The middleware classes still receive the string representation of the value.

Using a middleware class

// before

Route::etc()->middleware([
    MyMiddleware::class.':'.implode(',', [Foo::ONE, Foo::TWO]),
]);

// after

Route::etc()->middleware([
    MyMiddleware::class => [Foo::ONE, Foo::TWO],
]);

Using a middleware alias

// before

Route::etc()->middleware([
    'myAlias:'.implode(',', [Foo::ONE, Foo::TWO]),
]);

// after

Route::etc()->middleware([
    'myAlias' => [Foo::ONE, Foo::TWO],
]);

Optional single value API

You may omit the array if it is a single value.

// before

Route::etc()->middleware([
   'alias:'.Foo::ONE,
]);

// after

Route::etc()->middleware([
    'alias' => Foo::ONE,
]);

Handles backed enums

Remember that all values are still cast to a string. The middleware does not receive the enum.

// before

Route::etc()->middleware([
   'alias:'.Suit::Diamonds->value,
]);

// after

Route::etc()->middleware([
    'alias' => Suit::Diamonds,
]);

@timacdonald timacdonald marked this pull request as ready for review February 22, 2023 06:18
@emargareten
Copy link
Contributor

Note This is a wrapped around the stringy API. The middleware classes still receive the string representation of the value.

Why shouldn't we have the option to pass in any type?

@rodrigopedra
Copy link
Contributor

Why shouldn't we have the option to pass in any type?

I guess due to route serialization, when caching routes.

@timacdonald
Copy link
Member Author

timacdonald commented Feb 22, 2023

The root reason, IMO, it that it would be a breaking change for user-land and 3rd party middleware.

Middleware expect only strings, and may be typed as such. As much as I would like to investigate allowing rich type serialization / deserialization, it fundamentally changes middleware and feels like a major version change.

@taylorotwell
Copy link
Member

taylorotwell commented Mar 2, 2023

Keeping it real - I honestly find your package better. This feels like just trades the loose string approach for a loose array approach. Would prefer adding context specific builders to the applicable middleware, with real methods and arguments:

->middleware([Authenticate::using('web')]);

Basically would rather go through our existing built-in middleware and add those types of methods where it makes sense.

@timacdonald
Copy link
Member Author

I appreciate realness being kept.

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 this pull request may close these issues.

4 participants