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

Route Middleware stack #1622

Closed
mathmarques opened this issue Nov 26, 2015 · 4 comments
Closed

Route Middleware stack #1622

mathmarques opened this issue Nov 26, 2015 · 4 comments
Labels

Comments

@mathmarques
Copy link
Contributor

Suppose we have this:

$app->add(new \App\Middleware\MidTest('App Level Middleware1'));
$app->add(new \App\Middleware\MidTest('App Level Middleware2'));

$app->group('/foo', function() {

    $this->group('/bar', function() {

        $this->get('/', function () {

            return $this->response->getBody()->write("Page inside Group /foo Group /bar Get /! <br>");

        })->add(new \App\Middleware\MidTest('Get / Middleware 1'))->add(new \App\Middleware\MidTest('Get / Middleware 2'));

    })->add(new \App\Middleware\MidTest('Group /bar Middleware 1'))->add(new \App\Middleware\MidTest('Group /bar Middleware 2'));

})->add(new \App\Middleware\MidTest('Group /foo Middleware 1'))->add(new \App\Middleware\MidTest('Group /foo Middleware 2'));

App Level Middlewares are always executed first, since they are on app level. Ok.

Group /foo Middlewares will execute before Group /bar Middlewares. Ok.

The question:
Why Get / Middlewares will execute before Group /foo Middlewares ?
Shouldn't be executed after the Group /bar Middlewares?

I was expecting this order:

App Level Middleware2
App Level Middleware1
Group /foo Middleware 2
Group /foo Middleware 1
Group /bar Middleware 2
Group /bar Middleware 1
Get / Middleware 2
Get / Middleware 1
Page inside Group /foo Group /bar Get /!

But I got:

App Level Middleware2 
App Level Middleware1
Get / Middleware 2
Get / Middleware 1
Group /foo Middleware 2
Group /foo Middleware 1
Group /bar Middleware 2
Group /bar Middleware 1
Page inside Group /foo Group /bar Get /! 

I fixed this changing the \Slim\Route->finalize() to:

public function finalize()
    {
        $groupMiddleware = [];
        foreach ($this->getGroups() as $group) {
            foreach ($group->getMiddleware() as $middleware) {
                array_unshift($groupMiddleware, $middleware);
            }
        }

        $this->middleware = array_merge($this->middleware, $groupMiddleware);

        foreach ($this->getMiddleware() as $middleware) {
            $this->addMiddleware($middleware);
        }
    }

If this is a bug(what I think it is), I can make PR.
Commits here: https://github.com/mathmarques/Slim/commits/fix-route-middleware

@geggleto
Copy link
Member

Slim's middleware operates as LAST IN FIRST EXECUTED [LIFE].
The output is expected and correct.

@akrabat akrabat added the Slim 3 label Nov 28, 2015
@akrabat
Copy link
Member

akrabat commented Nov 28, 2015

Intuitively, I think that this is a bug too as your expected order matches what I expected too.

@mathmarques
Copy link
Contributor Author

I think it is too @akrabat.
Route Middleware on Slim 2(even being a little different) would match my expected order.
What could we do?
Wait for more feedbacks on this?

@akrabat
Copy link
Member

akrabat commented Nov 28, 2015

Please PR a fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants