Equivalent to Laravel's web and api middleware groups (named) #4699
Unanswered
gregmsanderson
asked this question in
Help
Replies: 1 comment
-
A thought: since a named collection only appears to support importing a single one, I figured I could do: export const middleware = router.named({
api: () => import('#middleware/api_middleware'),
web: () => import('#middleware/web_middleware'),
}) So far, so good! Those can be made by running:
So then the question: what should go in those classes (to apply the existing middleware, rather than add my own logic). For example in import type { HttpContext } from '@adonisjs/core/http'
import type { NextFn } from '@adonisjs/core/types/http'
export default class WebMiddleware {
async handle(ctx: HttpContext, next: NextFn) {
// question: what should go here? This is wrong
const sessionMiddleware = await import('@adonisjs/session/session_middleware')
//await new sessionMiddleware.default().handle(ctx, next)
const shieldMiddleware = await import('@adonisjs/shield/shield_middleware')
//await new shieldMiddleware.default().handle(ctx, next)
/**
* Call next method in the pipeline and return its output
*/
const output = await next()
return output
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
Since Adonis is inspired by Laravel, I wondered is there (or how to recreate) separate
web
andapi
middleware groups that contain common middleware to apply only to web or API routes? https://laravel.com/docs/11.x/middleware#laravels-default-middleware-groupsI see Adonis has a server middleware stack, but that's run on every request.
It has a router middleware stack, but that's run on every route.
The closest option appears the named middleware collections https://docs.adonisjs.com/guides/basics/middleware#named-middleware-collection. So I wondered about doing something like this ...
... to make a named middleware collection which could then be applied to just API routes. But that is a type error and it doesn't work. How can you use an array of middlware, the way that the provided stacks do?
Beta Was this translation helpful? Give feedback.
All reactions