| name | create-module |
| description | Create a new Modugo module with routes, binds, and imports |
Create a Modugo Module
A module in Modugo extends Module and implements three methods: imports(), binds(), and routes().
Steps
- Create a class that extends
Module
- Declare dependencies in
binds() using i.register*
- Declare routes in
routes() using the declarative DSL
- Import sub-modules in
imports() if needed
Basic module
final class ProfileModule extends Module {
@override
List<Type> imports() => []; // shared modules to import
@override
void binds() {
i.registerFactory<ProfileRepository>(() => ProfileRepositoryImpl());
i.registerFactory<ProfileViewModel>(() => ProfileViewModel(i.get()));
}
@override
List<IRoute> routes() => [
child('/', builder: (_, _) => const ProfilePage()),
child('/edit', builder: (_, _) => const EditProfilePage()),
];
}
Module with guard
final class ProfileModule extends Module {
@override
void binds() {
i.registerFactory<ProfileRepository>(() => ProfileRepositoryImpl());
}
@override
List<IRoute> routes() => [
child(
'/',
builder: (_, _) => const ProfilePage(),
guards: [AuthGuard()],
),
];
}
Registering the module in the parent
@override
List<IRoute> routes() => [
child('/', builder: (_, _) => const HomePage()),
module('/profile', ProfileModule()),
];
Notes
- All binds registered in a module are available via
i.get<T>(), Modugo.i.get<T>(), or context.read<T>()
- Guards defined on a
module() route propagate automatically to all child routes
- Use
imports() to share a module's binds with child modules without re-registering