[v5] Import files routes into routes/index and create a global group #3113
-
I would like to create a group with a prefix that encompasses all routes/groups, but I'm putting each route into a different file, and importing it into a route/index.ts. // project route file
const group = Route.group(() => {
Route.resource('projects', 'Project/MainController').apiOnly()
Route.get('project/medias', 'Project/Medias.index')
}).prefix('p') //index route
Route.group(()=>{
import('./project.ts')
}).prefix('api/v1') I would like the generated route to be something like:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
You can define your Route and export as function like these: // Using resource:
import Route from '@ioc:Adonis/Core/Route'
export default function userRoutes() {
Route.resource('users', 'UsersController').apiOnly()
}
// Using methods:
import Route from '@ioc:Adonis/Core/Route'
export default function authenticationRoutes() {
Route.post('login', 'LoginController.store')
Route.post('logout', 'LogoutController.store').middleware('auth')
}
// Using group:
import Route from '@ioc:Adonis/Core/Route'
export default function companyRoutes() {
Route.group(() => {
Route.get('companies/user', 'Companies/SearchController.index')
Route.resource('companies', 'Companies/CompaniesController').apiOnly().middleware({'*': 'bouncer: adm'})
}).middleware(['auth'])
} All the middlewares still working as they should Then you just need to import the functions in the /routes/index.ts file: import Route from '@ioc:Adonis/Core/Route'
import Env from '@ioc:Adonis/Core/Env'
import authenticationRoutes from './authentication'
import companyRoutes from './company'
import userRoutes from './user'
Route.group(() => {
companyRoutes()
userRoutes()
authenticationRoutes()
}).prefix('your/prefix/here') |
Beta Was this translation helpful? Give feedback.
-
Hello, |
Beta Was this translation helpful? Give feedback.
-
Hello, I have an update for Adonis version 6. In the preloads section, you can see the router being called.
Now, we have a new file called api create api.ts in start:
Note |
Beta Was this translation helpful? Give feedback.
You can define your Route and export as function like these: