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.3] Controller closure middleware #15080

Merged
merged 3 commits into from
Aug 26, 2016

Conversation

JosephSilber
Copy link
Member

@JosephSilber JosephSilber commented Aug 26, 2016

This will allow registering a closure middleware in the controller's constructor, so that any auth/session stuff can be called and set as properties on the controller:

public function __construct()
{
    $this->middleware(function ($request, $next) {
        $this->user = $request->user();

        return $next($request);
    });
}

Notes:

  1. To allow middleware closures, I had to rework the router's sortMiddleware method to not use collections, since contains and search do something else (invoke it) when passed a closure. That method looks a little nasty now 😕
  2. There's a small change which I'm not sure would be considered breaking: the middleware property on the controller is now stored in a different format. I don't think this is meant to be tinkered with directly, but who knows?

@malhal
Copy link
Contributor

malhal commented Sep 9, 2016

Hi @JosephSilber just a quick question, is it reasonable to make use of a middleware class in this new way:

public function __construct()
{
    $this->middleware(function ($request, $next) {
         return (new MyMiddleware())->handle($request, $next);
    });
}

@JosephSilber
Copy link
Member Author

JosephSilber commented Sep 9, 2016 via email

@malhal
Copy link
Contributor

malhal commented Sep 9, 2016

Ha thanks @JosephSilber I did not know that! I've just done a PR to add to the Controller page examples of both of these.

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.

3 participants