You can see here :

Location controllers :

You can find or add controllers in bundle repository under controller directory.

$ ls -l src/bundles/nameBundle/

total 24
drwxr-xr-x  2 cci  staff    68 20 jui 11:07 Command
drwxr-xr-x  2 cci  staff    68 20 jui 11:07 Entity
drwxr-xr-x  6 cci  staff   204 22 jui 13:57 Resources
drwxr-xr-x  4 cci  staff   136  6 oct 11:17 controller
drwxr-xr-x  2 cci  staff    68 20 jui 11:07 core
drwxr-xr-x  5 cci  staff   170 22 sep 14:23 doc
-rwxr-xr-x  1 cci  staff  1751 22 sep 10:56 nameBundle.js
-rwxr-xr-x  1 cci  staff   195 22 sep 10:56 package.json
-rwxr-xr-x  1 cci  staff   706 22 sep 10:56 readme.md
drwxr-xr-x  2 cci  staff    68 20 jui 11:07 services

	

File Controller :

File Controller :

The name of file must finish by '...Controller.js' example : nameController.js

$ ls -l src/bundles/nameBundle/controller

total 32
-rwxr-xr-x  1 cci  staff  8210  6 oct 10:58 defaultController.js
-rw-r--r--  1 cci  staff  2521 29 sep 14:44 gitController.js
	

Autoloader :

The core autoloader nodefony load all files in this directory which match with '...Controller.js'

Interpreter :

The core autoloader nodefony interpret the javascript code in an extended global context.
You can find namespace nodefony in the global context .

Class Controller :

const tab = [1,2,3,4];
const myFuntion = function(){
	return tab ;
}
/*
 *	Class myController
 *
 */
module.exports = class myController extends nodefony.controller {
	/*
	 *	CONSTRUCTOR
	 */
	constructor (container, context){
		super(container, context);
	};

	/*
 	*	Action controller
 	*	( action render public  )
 	*
 	*/
	indexAction(){
		let settings = this.kernel.settings ;
		let html = '<html><body>Nodefony version : '+settings.system.version+'</body></html>';
		return this.renderResponse(html);
	};
};

});
	

Controller Actions :

The nodefony core router find routes with pattern 'nameBundle:controller:action' Example : demoBundle:default:index
For the router find the controller's action functions.
The name of function must finish by '...Action' 'indexAction'

/*
 *	Action index in controller default
 *
 */
indexAction (){
	...
};
	

Rendering Datas :

There are 2 differents ways to render datas (or templates) in the actions of the controllers
Synchronous rendering

The controller give somme tools to render Synchrone data :

  • renderResponse
  • render
  • renderJson


/*
 *	RENDER RAW DATA WITH renderResponse
 */
indexAction (){
	var settings = this.kernel.settings ;
	var html = '<html><body>Nodefony version : '+settings.system.version+'</body></html>';
	return this.renderResponse(html);
};
					
/*
 *	RENDER TWIG TEMPLATE with render
 */
indexAction (){
	return this.render("demoBundle:Default:index.html.twig",{
		settings:this.kernel.settings
	});

};
					
/*
 *	RENDER TWIG TEMPLATE AUTO with object
 */
indexAction (){
	return {
		settings:this.kernel.settings
	}
};
					

Asynchronous rendering

The controller give somme tools to render Asynchrone data :
  • renderResponse
  • renderAsync
  • renderJsonAsync
The framework wait an event of other entity of framework ( orm database , syscall )


/*
 *	RENDER RAW DATA
 */
rawResponseAsyncAction(){
	var settings = this.kernel.settings ;

	// SYSCALL async
	var childHost =  exec('hostname', (error, stdout, stderr) => {
		var hostname = stdout ;

		var content = '<xml>'<nodefony>\
			'<kernel name="'+settings.name+'" version="'+settings.system.version+'">\
			'<server type="HTTP" port="'+settings.system.httpPort+'">'</server>\
			'<server type="HTTPS" port="'+settings.system.httpsPort+'">'</server>\
			'<hostname>'+hostname+'\
			'</kernel>\
			'</nodefony>'</xml>';

		this.renderResponse(content, 200 , {
			"content-type" :"Application/xml"
		})
	});

	return null;
};

					

Request Context :

Nodefony Context

In controller the actions can have two execution contexts.
When a request comes, the nodefony core instantiates a context Class ( http or websocket )

/*
*	 Detect  request type in action
*
*/
indexAction (){

	// two methods to detect the  request type by context or by method

	// FIRST with getContext
	var context = this.getContext();
	switch( context.type ){
		case "HTTP" :
			// HTTP CONTEXT
		break;
		case "HTTPS" :
			// HTTP CONTEXT
		break;
		case "WEBSOCKET" :
			// WEBSOCKET CONTEXT
		break;
		case "WEBSOCKET SECURE" :
			// WEBSOCKET CONTEXT
		break;
		default :
			throw new Error(" Bad Context")
	}

	// SECOND with getMethod
	var method = this.getMethod();
	switch( method ){
		case "GET" :
			// HTTP CONTEXT
		break;
		case "POST" :
			// HTTP CONTEXT
		break;
		case "DELETE" :
			// HTTP CONTEXT
		break;
		case "WEBSOCKET" :
			// WEBSOCKET CONTEXT
		break;
		default :
			throw new Error(" METHOD NOT ALLOWED")
	}
};
					

CONTEXT HTTP / HTTPS :

//  DUMP HTTPS CONTEXT

var context = this.getContext();
console.log(context);

Http {
type: 'HTTPS',
container: [Object],
notificationsCenter: [Object],
resolver: [Object],
request: [Object],
response: [Object],
session: [Object],
sessionService: [Object],
sessionAutoStart: 'firewall',
cookies: [Object],
isAjax: false,
secureArea: null,
showDebugBar: true,
kernel: [Object],
autoloadCache: [Object],
kernelHttp: [Object],
domain: 'nodefony.com',
validDomain: true,
security: [Object],
user: [Object],
url: 'https://nodefony.com:5152/demo/json',
remoteAddress: '192.168.100.71',
extendTwig: [Object],
profiling: [Object]
}
					

CONTEXT WEBSOCKET / WEBSOCKET SECURE :

// DUMP WEBSOCKET SECURE CONTEXT

var context = this.getContext();
console.log(context);

websocket {
type: 'WEBSOCKET SECURE',
container: [Object],
kernel: [Object],
autoloadCache: [Object],
kernelHttp: [Object],
request: [Object],
origin: 'https://nodefony.com:5152',
connection: [Object],
response: [Object],
originUrl: [Object],
secureArea: null,
cookies: [Object],
domain: 'nodefony.com',
validDomain: true,
session: [Object],
sessionService: [Object],
sessionAutoStart: 'firewall',
security: [Object],
user: [Object],
url: '/demo/websoket',
remoteAdress: '192.168.100.71',
resolver: [Object],
notificationsCenter: [Object],
extendTwig: [Object],
profiling: [Object]
}
					

Services Container :

Controller Services Container

You can find all services in controller
The Base controller give somme method to manage services container

  • this.get()
  • this.set()
  • this.getParameters()
  • this.setParameters()


// GET SETTINGS services HTTPS SERVER
var settingsHttps =  this.get("httpsServer").settings ;

// GET I18n services
var translateService = this.get("translation");

// GET sessions services
var sessionsService = this.get("sessions");
					

// get  asseticBundle globals settings
this.getParameters("bundles.assetic")

// LOG
{
	name: 'asseticBundle',
	locale: 'en_en',
	version: '1.0',
	less: { optimization: 0, compress: true, cache: true, async: false },
	coffescript: null,
	CDN: { stylesheets: null, javascripts: null }
}
					

Request and Response Object:

Request :

Request

//  DUMP Request

var resquest = this.getRequest();
console.log(resquest);

Request {
  container: [Object],
  request: [Object],
  headers: [Object],
  host: 'nodefony.com:5152',
  hostname: 'nodefony.com',
  sUrl: 'https://nodefony.com:5152/demo/json',
  url: [Object],
  queryPost: {},
  queryFile: {},
  queryGet: {},
  query: {},
  method: 'GET',
  rawContentType: {},
  contentType: null,
  charset: 'utf8',
  domain: 'nodefony.com',
  remoteAdress: '192.168.100.71',
  data: [],
  dataSize: 0,
  body: <Buffer>
}
					

Query String :

// Get query string from container service :
var queryString  = this.getParameters("query.get");

// Get query string from Class request
var request = this.getRequest();
var queryString = request.queryGet ;
						

Post Data :

// Get Post data from container service :
var post = this.getParameters("query.post");

// Get Post Data from Class request
var request = this.getRequest();
var postData = request.queryPost ;
						

Files Data : ( upload )

// Get Files data from container service :
var queryFile = this.getParameters("query.files");

// Get Files data  from Class request
var request = this.getRequest();
var queryFile = request.queryFile ;
						

Query Data :

// Get query  from container service :
// query is extend object with all request datas (get, post, files)
var query = this.getParameters("query.request");

// Get query  from Class request
var request = this.getRequest();
var query = request.query ;
						

Request Body :

// Get body from Class Request
var request = this.getRequest();
var body = request.body ;
						

Request Cookies :

var context = this.getContext();
var cookies = context.cookies ;
						


					
Response

// DUMP Response

var response = this.getResponse();
console.log(response);

Response {
  container: [Object],
  kernel: [Object],
  response: [Object],
  body: '',
  encoding: 'utf8',
  cookies: {},
  headers: {},
  ended: false,
  statusCode: 200,
  timeout: 40000
}
					

Response Set Timeout :

// response Timeout  when the time is up the framework makes the 408 response
this.getResponse().setTimeout(5000)
					

Response Set Headers :


// with setHeader
this.getResponse().setHeader("Content-Type", "text/html; charset=utf-8");

//or with setHeaders
this.getResponse().setHeaders({
	"Content-Type" :"text/html; charset=utf-8"
})
					

Response Set StatusCode :


//
this.getResponse().setStatusCode(200, "OK") ;
					

Response Add cookies :


var cookieDefaultSettings = {
	maxAge:24*60*60,
	path:"/",
	domain:null,
	secure:false,
	httpOnly:true,
};

var cookie = new nodefony.cookies.cookie("COOKIE NAME", "cookie value" , cookieDefaultSettings);
this.getResponse().addCookie(cookie);
					


					

Controller Logger :

indexAction (){
	var settings = this.kernel.settings ;
	this.logger( "Nodefony version : Alpha" , "INFO");
	var html = '<html><body>Nodefony version : '+settings.system.version+'</body></html>';
	return this.renderResponse(html);
};

// IN TERMINAL

Thu Oct 06 2016 15:49:10 INFO CONTROLER demo   : Nodefony version : Alpha