Build a blog with Areto Framework

Base controller

It is convenient to have a common controller methods collected in a single class. This purpose is served by the module/admin/component/Controller.js base controller in the admin module. The rest of the module controllers inherit from it directly or indirectly. The components module (application) directory contains the classes used in the models, controllers and views.


const Base = require('areto/base/Controller');

module.exports = class Controller extends Base {
  // place methods here

The getModel method finds the object by the ID passed in GET parameter. The getModelClass method returns the model class. The found model will be loaded together with the relations, listed in the relations array. If a model is not found, the method will throw a 404 error.


async getModel (params) {
  params = {
    ModelClass: this.getModelClass(),
    id: this.getQueryParam('id'),
  if (!MongoHelper.isValidId( {
    throw new BadRequest;
  let model = new params.ModelClass({module: this.module});
  model = await model.findById(;
  if (!model) {
    throw new NotFound;
  return model;

createDataProvider (config) {
  return new ActiveDataProvider({
    controller: this,

async renderDataProvider (provider, template, data) {
  await provider.prepare();
  await this.render(template, data);

The getRefUrl method returns the address of the previous page. Address is retrieved from the request header or POST parameter.


getRefUrl () {
  let ref = this.isGet()
    ? this.getHeader('referrer')
    : this.getPostParam('referrer');
  return ref ? ref : '';

The backToRef method forwards the current request to the address obtained from the url argument or POST data.


backToRef (url = 'index') {
  this.redirect(this.getPostParam('referrer') || url);