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

[5.2] Apply middleware once even in nested groups #13037

Closed
wants to merge 1 commit into from
Closed

[5.2] Apply middleware once even in nested groups #13037

wants to merge 1 commit into from

Conversation

themsaid
Copy link
Member

@themsaid themsaid commented Apr 6, 2016

In a previous PR we handled the case of applying a middleware more than once using Route::middleware() however the case of nested groups wasn't handled.

Here's the case:

$router->group(['middleware' => ['web', 'foo']], function () use ($router) {
    $router->group(['middleware' => 'web'], function () use ($router) {
        $router->group(['middleware' => ['web', 'foo', 'baz']], function () use ($router) {
            $router->get('bar', function () { return 'hello'; });
        });
    });
});

The middlewares extracted from this case are:

'web', 'foo', 'baz'

Before this PR:

'web', 'foo', 'web', 'web', 'foo', 'baz'

@GrahamCampbell
Copy link
Member

This would be a major breaking change.

@themsaid
Copy link
Member Author

themsaid commented Apr 6, 2016

@GrahamCampbell explain please.

@GrahamCampbell
Copy link
Member

You already explained it. Your before and afters are different.

@themsaid
Copy link
Member Author

themsaid commented Apr 6, 2016

A PR that does the same was merged into 5.2 a few weeks ago #12911

It has the same effect if you use the Route::middleware() method, this PR handles the case where you don't use the method but have nested groups.

However even without this PR, in the same example above if you applied ->middleware(..) to the route, it'll extract only the unique middlewares from the route and the test will pass.

I don't see a case where applying a middleware more than once is needed.

@GrahamCampbell
Copy link
Member

Gah, I think we should revert that.

@mark86092
Copy link
Contributor

@GrahamCampbell Is duplicated middleware intentional for some purposes? If not, what I can understand here is that two results of $route->middleware() are different. Thank you for your explaination

@themsaid
Copy link
Member Author

themsaid commented Apr 6, 2016

I believe forcing unique middlewares is a safety net that prevents executing a code multiple times while it should be executed once. Anyway if someone is depending on a middleware being applied multiple times intentionally yes this would be considered breaking, it depends on how you see the matter: bug or feature.

@GrahamCampbell
Copy link
Member

Is duplicated middleware intentional for some purposes?

Incase someone wants to wrap something more than once. It can't be that common though. I'm fine with this for 5.3, but I think we should revert the existing break on 5.3.

@@ -435,6 +435,10 @@ public function mergeWithLastGroup($new)
*/
public static function mergeGroup($new, $old)
{
if (isset($new['middleware'])) {
$new['middleware'] = array_diff((array) $new['middleware'], (array) $old['middleware']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is $old['middleware'] always available here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no, corrected. Sorry.

@taylorotwell
Copy link
Member

Yeah let's think about this for 5.3, if at all. I'm inclined to not really limit people in this way at all.

@themsaid
Copy link
Member Author

themsaid commented Apr 7, 2016

@taylorotwell then I think #12911 should be reverted as it has the same effect. no?

@themsaid themsaid deleted the unique-middleware-nested-groups branch April 24, 2016 09:52
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