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

Allow specifying middlewares at the controller level. #15558

Merged
merged 1 commit into from
Jun 2, 2021

Conversation

ADmad
Copy link
Member

@ADmad ADmad commented May 26, 2021

While we can setup route based middlewares at times setting them can be
a bit cumbersome. If for e.g. you want a middleware to run for just one
action you would have to setup an extra route for it.

Middlewares for controllers provides a good convenience and also allow replacing
some components with middlewares.

P. S. Laravel allows setting up middleware for controller in a similar way.

@ADmad ADmad added this to the 4.3.0 milestone May 26, 2021
@ADmad ADmad force-pushed the controller-middlewares branch 3 times, most recently from 5a68500 to 33e6953 Compare May 26, 2021 18:35
src/Controller/Controller.php Show resolved Hide resolved
* @return void
* @psalm-var array{?only: string|array, ?except: string|array}
*/
public function middleware($middleware, array $options = [])
Copy link
Member

Choose a reason for hiding this comment

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

Should this method raise an exception if it is called after middleware have already been applied? It would be frustrating to use this method in a beforeRender and then think that Cake is 'broken' when you're using it wrong.

Copy link
Member Author

Choose a reason for hiding this comment

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

Could be start with just documenting the fact that middlewares must be set from initialize() only?

I don't think we would be able to check if the middleware queue was already created without adding a new property.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah we would need another property to track whether or not middleware could be added.

src/Controller/ControllerFactory.php Outdated Show resolved Hide resolved
While we can setup route based middlewares at times setting them can be
a bit cumbersome. If for e.g. you want a middleware to run for just one
action you would have to setup an extra route for it.

Middlewares for controllers provides a good conveninece and also replacing
some components with middlewares.
@ADmad
Copy link
Member Author

ADmad commented Jun 2, 2021

Can this be merged?

@markstory markstory merged commit 25ad76f into 4.next Jun 2, 2021
@markstory markstory deleted the controller-middlewares branch June 2, 2021 19:20
markstory added a commit to cakephp/docs that referenced this pull request Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants