Modules

In Assegai, modules are used to organize and manage the different components of an application, such as controllers, services, imports and exports. Modules are defined using classes and the #[Module] attribute.

Creating a module

To create a module in Assegai, you will need to define a class and use the #[Module] attribute. This attribute specifies several properties of the module, such as the providers, controllers, imports, exports, and config. For example:

src/Spears/SpearsModule.php
#[Module(
  providers: [SpearsService::class],
  controllers: [SpearsController::class],
  imports: [],
  exports: [],
  config: [],
)]
class SpearsModule
{
}

This declares a module class named SpearsModule, which has a single service provider (SpearsService), a single controller (SpearsController), and no imports or exports specified. The config property is an array of configuration values that can be used to provide additional information to the module.

Providers

The providers property is an array of classes that are used to provide services to the module. These classes are typically used to define service classes that can be injected into other components of the module.

#[Modules(
  providers: [PostsService::class, UsersService::class]
  ...
)]

Controllers

The controllers property is an array of classes that define controllers in the module. These controllers are responsible for handling incoming requests and returning responses to the client.

#[Modules(
  controllers: [UsersController::class]
  ...
)]

Imports and exports

The imports property is an array of classes or modules that should be imported into the current module. This allows the current module to use the exports of the imported modules.

The exports property is an array of classes or values that should be made available to other modules that import this module.

src/Core/CoreModule
use #[Modules(
  imports: [CommonModule::class],
  exports: [CommonModule::class],
)]
class CoreModule
{
}

Config

The config property is an array of configuration values that can be used to provide additional information to the module. This can include things like application-level settings, database connections, or other values that may be needed by the module.

Organizing with modules

Modules are a useful tool for organizing and managing the different components of an Assegai application. They are essentially collections of related components, such as controllers, providers, and config, that can be easily imported and exported into other parts of the application. This allows for a more organized and efficient way of managing the dependencies and interactions between different parts of the application.

By using modules, you can define a set of related components in a single, cohesive unit, which can make it easier to manage and maintain the application as it grows in complexity. For example, you can create a module for handling user authentication, which might include controllers for handling login and logout requests, a provider for managing the user session, and config for setting up the authentication flow. This module can then be easily imported into other parts of the application where authentication is needed, allowing you to easily reuse and manage these components.

Overall, modules can help to make your Assegai application more modular, organized, and scalable, making it easier to develop and maintain over time.