thinkjs/lib/Lib/Core/Dispatcher.class.js
2013-11-14 12:18:16 +08:00

103 lines
3.4 KiB
JavaScript

/**
* 路由调配
* @type {Object}
*/
var url = require("url");
var path = require("path");
var Dispatcher = module.exports = Class(function(){
return {
init: function(http){
this.http = http;
},
run: function(){
var group = "";
var controller = "";
var action = "";
tag("path_info", this.http);
var pathname = this.http.pathname;
var extname = path.extname(pathname);
//判断URL后缀
if (extname == C('url_html_suffix')) {
pathname = path.dirname(pathname) + "/" + path.basename(pathname, C('url_html_suffix'));
};
pathname = pathname.split("/").filter(function(item){
item = item.trim();
if (item) {
return item;
};
}).join("/");
//去除pathname前缀
if (C('pathname_prefix') && pathname.indexOf(C('pathname_prefix')) === 0) {
pathname = pathname.substr(C('pathname_prefix').length);
};
this.http.pathname = pathname;
if (pathname == 'favicon.ico') {
throw_error("favicon.ico", this.http);
return false;
};
if (!this.routerCheck()) {
var paths = pathname.split("/");
var groupList = C('app_group_list') || [];
if (groupList.length) {
if (groupList.indexOf(paths[0])) {
group = paths.shift();
};
};
var deny = C('app_group_deny') || [];
if (group && deny.length) {
if (deny.indexOf(_group)) {
throw_error("favicon.ico", this.http);
};
};
controller = paths.shift();
action = paths.shift();
if (paths.length) {
for(var i = 0,length = Math.ceil(paths.length)/2; i < length; i++){
this.http.query[paths[i * 2]] = paths[i * 2 + 1] || "";
}
};
this.http.group = Dispatcher.getGroup(group);
this.http.controller = Dispatcher.getController(controller);
this.http.action = Dispatcher.getAction(action);
};
return true;
},
routerCheck: function(){
return tag('route_check', this.http);
}
}
});
/**
* 获取controller
* @param {[type]} controller [description]
* @return {[type]} [description]
*/
Dispatcher.getController = function(controller){
controller = controller || C('default_controller');
if (C('url_controller_map')[controller]) {
return C('url_controller_map')[controller];
};
return ucfirst(controller);
};
/**
* 获取action
* @param {[type]} action [description]
* @return {[type]} [description]
*/
Dispatcher.getAction = function(action){
action = action || C('default_action');
if (C('url_action_map')[action]) {
return C('url_action_map')[action];
};
return action;
};
/**
* 获取group
* @param {[type]} group [description]
* @return {[type]} [description]
*/
Dispatcher.getGroup = function(group){
group = group || C('default_group');
return ucfirst(group);
}