-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Update middleware docs. #5096
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
Update middleware docs. #5096
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -878,9 +878,13 @@ to do automatic view switching based on content types. | |
| Connecting Scoped Middleware | ||
| ---------------------------- | ||
|
|
||
| Middleware can be applied to your entire application, or to an individual | ||
| routing scope. Before middleware can be applied to a scope, it needs to be | ||
| registered:: | ||
| While Middleware can be applied to your entire application, applying middleware | ||
| to specific routing scopes offers more flexibility, as you can apply middleware | ||
| only where it is needed allowing your middleware to not concern itself with | ||
| how/where it is being applied. | ||
|
|
||
| Before middleware can be applied to a scope, it needs to be | ||
| registered into the route collection:: | ||
|
|
||
| // in config/routes.php | ||
| use Cake\Http\Middleware\CsrfProtectionMiddleware; | ||
|
|
@@ -891,21 +895,61 @@ registered:: | |
| $routes->registerMiddleware('cookies', new EncryptedCookiesMiddleware()); | ||
| }); | ||
|
|
||
| Once registered into the route builder, middleware can be applied to specific | ||
| Once registered, scoped middleware can be applied to specific | ||
| scopes:: | ||
|
|
||
| $routes->scope('/cms', function ($routes) { | ||
| // Enable registered middleware for this scope. | ||
| // Enable CSRF & cookies middleware | ||
| $routes->applyMiddleware('csrf', 'cookies'); | ||
| $routes->get('/articles/:action/*', ['controller' => 'Articles']) | ||
| }); | ||
|
|
||
| In situations where you have nested scopes, inner scopes will inherit the | ||
| middleware applied in the containing scope:: | ||
|
|
||
| $routes->scope('/api', function ($routes) { | ||
| $routes->applyMiddleware('ratelimit', 'auth.api'); | ||
| $routes->scope('/v1', function ($routes) { | ||
| $routes->applyMiddleware('v1compat'); | ||
| // Define routes here. | ||
| }); | ||
| }); | ||
|
|
||
| In the above example, the routes defined in ``/v1`` will have 'ratelimit', | ||
| 'auth.api', and 'v1compat' middleware applied. If you re-open a scope, the | ||
| middleware applied to routes in each scope will be isolated:: | ||
|
|
||
| $routes->scope('/blog', function ($routes) { | ||
| $routes->applyMiddleware('auth'); | ||
| // Connect the authenticated actions for the blog here. | ||
| }); | ||
| $routes->scope('/blog', function ($routes) { | ||
| // Connect the public actions for the blog here. | ||
| }); | ||
|
|
||
| In the above example, the two uses of the ``/blog`` scope do not share | ||
| middleware. However, both of these scopes will inherit middleware defied in | ||
| their enclosing scopes. | ||
|
|
||
|
|
||
| Grouping Middleware | ||
| ------------------- | ||
|
|
||
| To help keep your route code :abbr:`DRY (Do not Repeat Yourself)` middleware can | ||
| be combined into groups. Once combined groups can be applied like middleware | ||
| can:: | ||
|
|
||
| $routes->registerMiddleware('cookie', new EncryptedCookieMiddleware()); | ||
| $routes->registerMiddleware('auth', new AuthenticationMiddleware()); | ||
| $routes->registerMiddleware('csrf', new CsrfProtectionMiddleware()); | ||
| $routes->middlewareGroup('web', ['cookie', 'auth', 'csrf']); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "web" seems a very general. How about "authenticated_site" or amything like that?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like |
||
|
|
||
| // Apply the group | ||
| $routes->applyMiddleware('web'); | ||
|
|
||
| In situations where you have nested scopes, all middleware applied in each scope | ||
| will be applied starting from the top-most scope and ending with the nested | ||
| scopes. By applying middleware in specific scopes you can omit complicated URL | ||
| matching logic out of your middleware layers and let them focus on their task. | ||
|
|
||
| .. versionadded:: 3.5.0 | ||
| Scoped middleware support was added in 3.5.0 | ||
| Scoped middleware & middleware groups were added in 3.5.0 | ||
|
|
||
| .. _resource-routes: | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... this gave me some laughs... :)
So now it is sometimes middlewareQueue and sometimes middlewareQueueStack ;)?
Personally I'd rename the core class to MiddlewareStack and alias it from middlewareQueue to be removed in 4.0. Stack is easier to type and speak.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a mistake. I will get that fixed.