TramwayJS

Controller

A class that orchestrates communications between services and the client.

Controllers link to actions from the routing and act to direct the flow of the application.

Definition

Locations

  • Implementation: src/controllers
  • Dependency Injection: src/config/services/controllers.js

API

The Controller class also contains some helper functions that can be used by any child Controller - including RestfulController.

FunctionUsage
getRouter(): RouterReturns the Router class for extendability
redirect(res: Object, path: string, status: number)Calls the main redirect function in Express. Will default to a 301 status code.
getRoute(name: string)Gets route metadata by name.
getRouteByAction(action: string)Gets route for the current controller action where action is the method name.

Example

import {Controller} from 'tramway-core-router';
export default class MainController extends Controller {
async index(req, res, next) {
return res.send('Welcome to TramwayJS, this is the index action');
}
}

Dependency Injection

import {
MainController,
} from '../../controllers';
export default {
"controller.main": {
"class": MainController,
"constructor": [
{"type": "service", "key": "router"}
],
"functions": []
},
};