BaseController - jellyfishsolutions/lynx GitHub Wiki
The BaseController class is the base class for any controller.
This class can be used for two purposes.
The main purpose is to define a controller that contains actions that responds to routes, using the Route annotation combined with the verbs annotations (GET, POST, ...). In this case, the class shall be defined inside a controllers folder of a module, and exported as default class.
The secondary purpose is to add functionalities to the BaseController, without adding actions, in order to enable the creation of other controllers.
Any controller is unique inside Lynx, and any controller is not recreated to manage the a request.
This method is called only when the constructed has been completed. Since this method is async, it can be used to perform some initialization that needed the use of the await keyword. It is possible to override this method to perform any async initialization.
We typically use this method to create a logic folder inside the Media entity system, in order to manage uploaded media.
const UPLOAD_PATH = '/cms_assets/';
@Route('/admin/assets')
export default class AssetController extends BaseController {
private static folder: Media;
async postConstructor() {
await super.postConstructor();
AssetController.folder = await Media.mkdir(UPLOAD_PATH);
}
...
}
Add a value to the current request context. Any variable added with this method will available in the template context thought the render method.
Parameters:
- req: the current Request
- key: the key of the value to add
- value: the value to add
@Route("/")
export default class MainController extends BaseController {
@Name("fe_index")
@AsyncVerify(authUser)
@GET("/")
async goIndex(req: Request): Promise<Response> {
this.addToContext(req, "title", this.tr("fe.titles.home-page", req));
return this.render("frontend/index", req);
}
}Inside the template:
{% block body %}
<h1 class="h3 mb-0 text-gray-800">{{ title }} </h1>
{% endblock %}This method generates a url to a route starting from the route name and optionally its parameters. If a parameter not is used to generate the route url, it will be appended as a query parameter.
Parameters:
- name: the name of the route
- parameters: a plain object containing the parameters for the route.
With the following method, annotated with Name,
...
@Name('ajax-selection')
@API()
@GET('/ajaxRequest/:entityName/:id/:field')
async ajaxRequest(
entityName: string,
id: any,
field: string,
req: Request
) {
return this.performAjaxRequest(entityName, id, field, req);
}this.route('ajax-selection', { entityName: 'name', id: 1, field: 'test' }) results to /ajaxRequest/name/1/test.
this.route('ajax-selection', { entityName: 'name', id: 1, field: 'test', page: 5, order: 'creation' }) results to /ajaxRequest/name/1/test?page=5&order=creation.
this.route('no_tagged_name', { page: 5, id: 1 }) results to no_tagged_name?page=5&id=1.
Add a flash message in the current request.
Internally, the express-flash-message is used.
Usually, addSuccessMessage and addErrorMessage are used instead of this method.
Add a success flash message in the current request.
Parameters:
- msg: the string (can be localized) of the message
- req: the request
Add an error flash message in the current request.
Parameters:
- msg: the string (can be localized) of the message
- req: the request
Utility method to obtain a translated string.
Parameters:
- str: the string key to be translated
- req: the original request
- language: optionally, the language can be forced using this variable
Utility method to obtain a translated string, formatted with parameters. Each parameter should be encoded as {0}, {1}, etc...
Parameters:
- str: the string key to be translated
- req: the original request
- language: optionally, the language can be forced using this variable
- args: the arguments to format the string
The BaseController class defines methods to directly send emails, using the client configured globally in the application.
Utility method to send emails from a controller.
This method is similar to the sendMail method, but define a lower level API. Indeed, it directly accepts the text and the html of the email, and not the templates urls.
Parameters:
- dest: the email destination (can also be an array of addresses)
- subject: the subject of the email
- text: the text version of the email
- html: the html version of the email
...
await this.sendRawMail("[email protected]", "Subject of the mail", "Text of the email", "<br>Html text of the <b>email</b>");async sendMail(req: express.Request, dest: string | string[], subjectTemplateString: string, textTemplate: string, htmlTemplate: string, context: any): Promise<boolean>
Utility method to send an email from a controller. This method uses the template engine to compile the email, for text and html body and for the subject.
This method works as the render method to generate a standard html page, but for emails.
Parameters:
- req: the current request
- dest: the email destination (can also be an array of addresses)
- subjectTemplateString: the subject of the email, that can also be a string template
- textTemplate: the text version of the email, referencing a path in the view folders
- htmlTemplate: the html version of the email, referencing a path in the view folders
- context: a plain object containing any necessary data needed by the view