Routes in ModularBr: how to define and manage your routes.
The ModularBr features a powerful route management capability that supports requests using the GET, POST, PUT, DELETE, and PATCH methods, following the REST architecture. Routes are added to modules through the construction of the Route class, where the method, path, and route handler are specified.
To illustrate the use of routes in ModularBr, let's consider the AppModule and add some routes to it, assuming you are using a request framework like Horse.
THorse.Get('/nfe/:id',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
begin
Modular.LoadRouteModule('/nfe/:id');
try
// Make use of the features of the NFeModule module here...
Res.Send('ModularBr' + Modular.Get<TControllerServer>.GetNfe)
.Status(200)
.ContentType('application/json');
finally
Modular.DisposeRouteModule('/nfe/:id');
end;
end);
app.module.pas
unit app.module;
interface
uses
dmfbr.module,
nfe.module, cte.module, nfse.module, nfce.module, mdfe.module;
type
TAppModule = class(TModule)
public
function Routes: TRoutes; override;
end;
implementation
{ TAppModule }
function TAppModule.Routes: TRoutes;
begin
// Routes
Result := [RouteModule('/nfe/:id', TNFeModule)];
end;
end.
nfe.module.pas
unit nfe.module;
interface
uses
dmfbr.module,
nfe.repository, nfe.controller, nfe.provider;
type
TNFeModule = class(TModule)
public
function Routes: TRoutes; override;
end;
implementation
{ TNFeModule }
function TNFeModule.Binds: TBinds;
begin
// Dependency injection
Result := [Bind<TRepositoryServer>.SingletonLazy,
Bind<TControllerServer>.Singleton,
Bind<TProviderORMBr>.Factory];
end;
function TNFeModule.Routes: TRoutes;
begin
// Exemple: [RouteModule('/nfe/pdf/:id', TPDFModulo),
// RouteModule('/nfe/xml/:id', TXMLModulo)]
Result := [];
end;
end.
Now you can test it in your browser or using a program like Postman.