Examples :

Rendering html :

Simple Render html

URL : http://localhost:5151/doc/demo/html/nodefony

documentation-demo:
  pattern:  /doc/demo/html/{name}
  defaults: {controller: "documentation-bundle:demo:html", "name":"nodefony"}
					

/*
 *	Class demoController
 */
module.exports = class demoController extends nodefony.controller {
	/*
	 *	CONSTRUCTOR
	 */
	constructor (container, context){
		super(container, context);
	}
	/*
 	*	Action controller
 	*
 	*/
	htmlAction (name){
		return this.renderResponse('<h1>'+name+'</h1>');
	}
};
					

nodefony

Rendering Templates :

Rendering Twig Templates

URL : http://localhost:5151/doc/demo/render/nodefony

documentation-demo-render:
  pattern:  /doc/demo/render/{name}
  defaults: {controller: "documentation-bundle:demo:render", "name":"nodefony"}
					

/*
 *	Class demoController
 *
 */
module.exports = class demoController extends nodefony.controller {
	/*
	 *	CONSTRUCTOR
	 */
	constructor (container, context){
		super(container, context);
	}
	/*
 	*	Action controller
 	*
 	*/
	renderAction (name){
		return this.render('documentation-bundle:demo:index.html.twig', {
			name:name,
		});
	}
};
					
<h1> {{name}} </h1>
					

nodefony

Rendering database query :

Rendering database query with Promise

URL : http://localhost:5151/query/join

sqlJoin:
  pattern:  /query/join
  defaults: {"controller": "demo-bundle:demo:querySqlJoin"}
  requirements:
    method: GET
					

/*
 *	Class demoController
 *
 */
module.exports = class demoController extends nodefony.controller {
	/*
	 *	CONSTRUCTOR
	 */
	constructor (container, context){
		super(container, context);
	}
	/*
 	*	Action controller
 	*/
	querySqlJoinAction (){
		let orm = this.getORM() ;
		let nodefonyDb = orm.getConnection("nodefony") ;

		return nodefonyDb.query('SELECT * FROM sessions S LEFT JOIN users U on U.id = S.user_id ')
		.then((result) => {
			let joins = result[0];
			for (let i = 0 ; i < joins.length ; i++){
				joins[i].metaBag = JSON.parse( joins[i].metaBag );
			}
			return this.render('demo-bundle:orm:orm.html.twig', {
				joins:joins,
			});
		})
	}
};
					
{% if joins %}
	<div class="container">
		<div class="row">
			<div class="card card-default ">
				<!-- Default card contents -->
				<div class="card-header bg-nodefony"><strong>SESSION  BY USER   </strong></div>
				<div class="card-body">

				<div class="table-responsive  " >
					<table class="table table-striped table-bordered">
						<thead>
							<tr>
								<th>USERNAME</th>
								<th>REQUEST</th>
								<th>SESSION TOKEN</th>
								<th>SESSION CONTEXT</th>
								<th>REMOTE ADRESS</th>
								<th>LOCALE</th>
							</tr>
						</thead>
						<tbody>
							{% for data in joins %}
							<tr>
								<td>{{data.username}}</td>
								<td>{{data.metaBag.request}}</td>
								<td>{{data.session_id}}</td>
								<td>{{data.context}}</td>
								<td>{{data.metaBag.remoteAddress }}</td>
								<td>{{data.lang}}</td>
							</tr>
							{% endfor %}
						</tbody>
					</table>
				</div>
				</div>
			</div>
		</div>
	</div>
{% endif %}
					

Rendering Websocket :

Rendering Websocket

URL : ws://localhost:5151/websoket

websoket:
  pattern:  /websoket
  defaults: {"controller": "demo-bundle:demo:websoket"}
  requirements:
    method:     GET,WEBSOCKET
					

/*
*	Class demoController
*
*/
module.exports = class demoController extends nodefony.controller {
	/*
	*	CONSTRUCTOR
	*/
	constructor (container, context){
		super(container, context);
	}
	/*
	*	Action controller
	*
	*/
	websoketAction (message){
		switch( this.getMethod() ){
			case "GET" :
				return this.render('demo-bundle:Default:websocket.html.twig',{name:"websoket"});
			break;
			case "WEBSOCKET" :
				if (message){
					// LOG  MESSAGE CLIENT IN TERMINAL
					this.logger( message.utf8Data , "INFO");
				}else{
					// PREPARE  PUSH MESSAGES SERVER
					// SEND MESSAGES TO CLIENTS
					let i = 0 ;
					let id = setInterval(() => {
						let mess = "I am a  message "+ i +"\n" ;
						 // You can use context to send data
						this.context.send(mess);
						//  or call controller method ( renderResponse , render , renderJson )
						this.renderResponse(mess);
						// LOG  MESSAGE SENDING IN TERMINAL
						this.logger( "SEND TO CLIENT :" + mess , "INFO");
						i++
					}, 1000);

					setTimeout(function(){
						clearInterval(id);
						// close reason , descripton
						this.context.close(1000, "NODEFONY CONTROLLER CLOSE SOCKET");
						id = null ;
					}, 10000);
					// Event websocket close by client
					this.context.listen(this, "onClose" , function(){
						if (id){
							clearInterval(id);
						}
					})
				}
			break;
			default :
				throw new Error(" METHOD NOT ALLOWED")
		}
	}
};
					


/* YOU CAN SHOW LOG IN TERMINAL */

Thu Oct 06 2016 15:49:09 INFO REQUEST WEBSOCKET SECURE  :  Connection Websocket Connection from : 192.168.100.71 PID :86607 ORIGIN : https://nodefony.com:5152
Thu Oct 06 2016 15:49:09 INFO CONTROLER demo   :  HELLO SERVER I AM A CLIENT : Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2879.0 Safari/537.36

Thu Oct 06 2016 15:49:10 INFO CONTROLER demo   : SEND TO CLIENT :I am a  message 0
Thu Oct 06 2016 15:49:11 INFO CONTROLER demo   : SEND TO CLIENT :I am a  message 1
Thu Oct 06 2016 15:49:12 INFO CONTROLER demo   : SEND TO CLIENT :I am a  message 2
Thu Oct 06 2016 15:49:13 INFO CONTROLER demo   : SEND TO CLIENT :I am a  message 3
Thu Oct 06 2016 15:49:14 INFO CONTROLER demo   : SEND TO CLIENT :I am a  message 4
Thu Oct 06 2016 15:49:15 INFO CONTROLER demo   : SEND TO CLIENT :I am a  message 5
Thu Oct 06 2016 15:49:16 INFO CONTROLER demo   : SEND TO CLIENT :I am a  message 6
Thu Oct 06 2016 15:49:17 INFO CONTROLER demo   : SEND TO CLIENT :I am a  message 7
Thu Oct 06 2016 15:49:18 INFO CONTROLER demo   : SEND TO CLIENT :I am a  message 8

Thu Oct 06 2016 15:49:19 INFO REQUEST WEBSOCKET SECURE  : Thu Oct 06 2016 15:49:19 GMT+0200 (CEST) Connection Websocket CLOSE : 192.168.100.71 PID :86607 ORIGIN : https://nodefony.com:5152 1000 NODEFONY CONTROLLER CLOSE SOCKET
					

Rendering JSON :

Rendering JSON

URL : http://localhost:5151/json

json:
  pattern:  /json
  defaults: {"controller": "demo-bundle:demo:json"}
					
/*
 *	Class demoController
 *
 */
module.exports = class demoController extends nodefony.controller {
	/*
	 *	CONSTRUCTOR
	 */
	constructor (container, context){
		super(container, context);
	}
	/*
 	*	Action controller
 	*
 	*/
	jsonAction (){
		return this.renderJson({
			foo:"bar",
			bar:"foo"
		});
	}
};
					

Rendering XML :

Rendering XML

URL : http://localhost:5151/xml

xmlResponse:
  pattern:  /xml
  defaults: {"controller": "demo-bundle:demo:xml"}
  requirements:
    method: GET

					
/*
 *	Class demoController
 *
 */
module.exports = class demoController extends nodefony.controller {
	/*
	 *	CONSTRUCTOR
	 */
	constructor (container, context){
		super(container, context);
	}
	/*
 	*	Action controller
 	*
 	*/
	xmlAction (){
		let settings = this.kernel.settings ;
		let 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>\
			</kernel>\
		</nodefony></xml>';
		return this.renderResponse(content, 200 , {
			"content-type" :"Application/xml"
		});
	}
};
					

Download File :

Download File

URL : http://localhost:5151/download

download:
  pattern:  /download
  defaults: {"controller": "demo-bundle:finder:download"}
  requirements:
    method: GET
					
/*
 *	Class finderController
 *
 */
module.exports = class finderController extends nodefony.controller {
	/*
	 *	CONSTRUCTOR
	 */
	constructor (container, context){
		super(container, context);
	}
	/*
 	*	Action controller
 	*
 	*/
	downloadAction (){
		let filePath = this.kernel.rootDir + '/README.md';
		return this.renderFileDownload(filePath);
	}
};
					

Media Stream Rendering :

Media Stream Rendering

URL : http://localhost:5151/rendermedia

render-media:
  pattern:  /rendermedia
  defaults: {"controller": "demo-bundle:demo:mediaStream"}
  requirements:
    method: GET

					
/*
 *	Class demoController
 *
 */
module.exports = class demoController extends nodefony.controller {
	/*
	 *	CONSTRUCTOR
	 */
	constructor (container, context){
		super(container, context);
	}
	/*
 	*	Action controller
 	*
 	*/
	mediaStreamAction (){
		let filePath = this.kernel.rootDir + '/app/Resources/medias/myAudio.mp3';
		let file = new nodefony.fileClass(filePath);
		return this.renderMediaStream(file);
	}
}
					

File Upload :

Upload Rendering

URL : http://localhost:5151/fileupload

uploadFile:
  pattern:  /fileupload
  defaults: {"controller": "demo-bundle:demo:upload"}
  requirements:
    method: POST
					
/*
 *	Class demoController
 *
 */
module.exports = class demoController extends nodefony.controller {
	/*
	 *	CONSTRUCTOR
	 */
	constructor (container, context){
		super(container, context);
	}
	/*
 	*	Action controller
 	*
 	*/
	uploadAction (){
		let files = this.getParameters("query.files");
		let myPath =  path.resolve( this.kernel.rootDir, "src", "bundles", "demo-bundle", "Resources", "images");
		for (let file in files){
			if( files[file].error ){
				throw files[file].error ;
			}
			files[file].move(myPath);
			//console.log( files[file].getExtension() )
			//console.log( files[file].getMimeType() )
			//console.log( files[file].realName() )
		}
		if ( ! this.isAjax() ){
			return this.redirect ( this.generateUrl("finder") );
		}else{
			let res = {
				"files": [],
				"metas": []
			}
			for (let file in files){
				let name = files[file].realName();
				res.files.push(myPath+"/"+name);
				let meta = {
					date : new Date(),
					extension:files[file].getExtension(),
					file:myPath+"/"+name,
					name:name,
					old_name:files[file].name,
					size:files[file].stats.size,
					size2:files[file].stats.size,
					type:files[file].getMimeType().split("/")
				}
				res.metas.push(meta);
			}
			return this.renderResponse(
				JSON.stringify(res),
				200,
				{'Content-Type': 'application/json; charset=utf-8'}
			);
		}
	}
};
					

Redirecting and Forwarding :

Redirecting and Forwarding

redirectGoogleAction (){
	// status 301 or 302
	return this.redirect("http://google.com");
};
					

forwardAction (){
	let docBundle = this.kernel.getBundles("documentation");
	if (  docBundle ){
		return this.forward("documentation-bundle:default:navDoc");
	}
	return this.render('demo-bundle:Default:navDoc.html.twig');
}
					
generateUrlAction (){
	// absolute
	return this.redirect ( this.generateUrl("user", {name:"cci"}, true) );

	// relative
	return this.redirect ( this.generateUrl("user", {name:"cci"} );
};