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

Make universal routes work for controller middleware #1151

Merged
merged 2 commits into from
Jan 25, 2024

Conversation

chillbram
Copy link
Contributor

This PR allows support for controller middleware. $route->computedMiddleware is equal to Router::uniqueMiddleware(array_merge($route->middleware(), $route->controllerMiddleware()));. So basically the same result as before, but with the controllerMiddleware added. I have added a test, which should cover this use case.

@chillbram
Copy link
Contributor Author

@stancl Could you take a look at this PR. For my projects this would be the last puzzle piece to make Tenancy v3 work with Livewire v3

@stancl
Copy link
Member

stancl commented Oct 9, 2023

Curious how you're using controller middleware with Livewire. Are you specifying it in the component in some way?

And is there a reason you can't use route middleware here?

@chillbram
Copy link
Contributor Author

Livewire v3 itself now uses controller middleware for the File Upload feature by using the getMiddleware() function. Now that this Livewire PR is merged, we can define ['universal', InitializeTenancyByDomain::class] inside the Livewire config and apply the correct tenancy middleware for the file upload routes.

With the way this feature is coded now, there is no other way to apply middleware to this route. A PR was opened to allow custom Route definitions, but that was closed due to issues with the implementation.

@stancl
Copy link
Member

stancl commented Oct 9, 2023

@chillbram Does your PR handle: this route as well? https://github.com/livewire/livewire/blob/1ec54cdf57c6f1bf734bb14f9732904473b91c3f/src/Features/SupportFileUploads/SupportFileUploads.php#L38

And wouldn't your array_map() lead to route definition like this:

[
    [
      "middleware" => "tenancy",
      "options" => [],
    ],
    [
      "middleware" => "universal",
      "options" => [],
    ],
]

Which I think should be:

[
    [
      "middleware" => ["tenancy", "universal"],
      "options" => [],
    ],
]

@chillbram
Copy link
Contributor Author

No it does not handle the preview file. I don't know why it doesn't implement middleware in the same way, but I've never used that feature and have no idea how it works.

You're right that the array_map forms the array in that way, but that's how Laravel wants it. I guess they want to allow options to be set for each individual middleware. I tested passing an array to the middleware array key, but Laravel does not support that.

@stancl
Copy link
Member

stancl commented Oct 9, 2023

I see, so the usage of options would be (in a generalized example) something like this?

Route::group([
    'prefix' => 'foo',
    'middleware' => [
        ['middleware' => FooMiddleware::class, 'options' => ['options for foo middleware']],
        ['middleware' => BarMiddleware::class, 'options' => ['options for bar middleware']],
    ],
], function () {
    //
});

@chillbram
Copy link
Contributor Author

I don't think your code example works when used like that on the Route:: facade. But if you'd move that middleware array to a getMiddleware() function in a controller, that would have the result you'd expect. Controller middleware isn't really documented I don't think, so we kind of have to live with their quirks.

@chillbram
Copy link
Contributor Author

@stancl Do you agree with this implementation and its test coverage?

@stancl
Copy link
Member

stancl commented Oct 15, 2023

We're looking into this at the moment. The solution used here is causing some issues with how we do things in v4, so we're looking at other ways to approach the problem.

@stancl
Copy link
Member

stancl commented Nov 6, 2023

@chillbram could you try if changing the Livewire code locally like this fixes the issue for you?

@chillbram
Copy link
Contributor Author

It works for files uploaded on a tenant, but not on the central domain. Universal routes don't seem to work with this implementation yet

@stancl
Copy link
Member

stancl commented Jan 11, 2024

I submitted a PR to Livewire with a similar implementation to the one I linked.

Once it's merged, if you could check whether the changes submitted here work with the new version of Livewire, I can merge this.

Our priority is Livewire 3 support in v4, but since this is essentially a one line PR and doesn't appear to break any tests, I can merge this into v3.

@chillbram
Copy link
Contributor Author

Just checked and this change still works with Livewire v3.4.1, so I think this is safe to merge into Tenancy v3

@stancl
Copy link
Member

stancl commented Jan 25, 2024

I added a fallback to middleware() in case this code was ever executed before computedMiddleware got filled (since the value of that property depends on the point in the lifecycle where the route is referenced, i.e. gatherMiddleware() needs to be called first).

@stancl stancl merged commit 5fe8825 into archtechx:3.x Jan 25, 2024
2 checks passed
@chillbram chillbram deleted the use-computed-middleware branch February 12, 2024 16:02
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.

2 participants