support sub controller

This commit is contained in:
lichengyin 2015-12-29 13:57:30 +08:00
parent 45d344b06b
commit d2d8d0f76e
3 changed files with 89 additions and 25 deletions

View File

@ -48,8 +48,8 @@ thinkData.template = {};
/**
* store sorted controllers in module, for parse route which support sub controllers
* {
* home: ['test/index', 'test', 'index', 'base']
* home: ['test/index']
* }
* @type {Object}
*/
thinkData.controller = {};
thinkData.subController = {};

View File

@ -312,6 +312,29 @@ export default class {
});
}
}
/**
* load sub controller
* @return {} []
*/
loadSubController(){
think.module.forEach(module => {
let filepath = think.getPath(module, think.dirname.controller);
let subControllers = think.getFiles(filepath).filter(item => {
if(item.indexOf(think.sep) === -1){
return;
}
if(path.extname(item) !== '.js'){
return;
}
return true;
}).map(item => {
return item.slice(0, -3).replace(/\\/g, '/');
});
if(subControllers.length){
thinkData.subController[module] = subControllers;
}
});
}
/**
* load bootstrap
* @return {} []
@ -390,6 +413,7 @@ export default class {
thinkData.hook = {};
thinkData.template = {};
thinkData.middleware = {};
thinkData.subController = {};
thinkData.route = null;
}
/**
@ -404,6 +428,7 @@ export default class {
this.loadAdapter();
this.loadMiddleware();
this.loadMVC();
this.loadSubController();
this.loadHook();
this.loadTemplate();
this.loadError();
@ -417,6 +442,7 @@ export default class {
think.toFastProperties(thinkData.middleware);
think.toFastProperties(thinkData.error);
think.toFastProperties(thinkData.template);
think.toFastProperties(thinkData.subController);
//console.log(thinkData.alias)
//console.log(eval('%HasFastProperties(thinkData.template)'))

View File

@ -111,41 +111,78 @@ export default class extends think.middleware.base {
}
this.http.pathname = pathname;
}
/**
* get module from pathname
* @return {String} []
*/
parseModule(){
let defaultModule = think.config('default_module');
if(think.mode === think.mode_normal){
return defaultModule;
}
let http = this.http;
let pathname = http.pathname;
let pos = pathname.indexOf('/');
let mod = pos === -1 ? pathname : pathname.substr(0, pos);
if(this.module){
if(this.module === mod){
http.pathname = pathname.substr(mod.length + 1);
}else {
mod = this.module;
}
}else if (mod && mod !== think.dirname.common && think.module.indexOf(mod) > -1) {
http.pathname = pathname.substr(mod.length + 1);
}else{
mod = '';
}
return mod || defaultModule;
}
/**
* get controller from pathname
* @return {} []
*/
parseController(module){
let subControllers = thinkData.subController[module];
let http = this.http;
let pathname = http.pathname;
if(!pathname){
return '';
}
//search sub controller
if(subControllers){
for(let i = 0 ,length = subControllers.length, item; i < length; i++){
item = subControllers[i];
if(pathname === item || pathname.indexOf(item + '/') === 0){
http.pathname = http.pathname.substr(item.length + 1);
return item;
}
}
}
let pos = pathname.indexOf('/');
let controller = pos === -1 ? pathname : pathname.substr(0, pos);
http.pathname = http.pathname.substr(controller.length + 1);
return controller;
}
/**
* parse pathname
* @return {} []
*/
parsePathname(){
let http = this.http, pathname = http.pathname;
if (!pathname) {
let http = this.http;
if (!http.pathname) {
this.http.module = this.getModule();
this.http.controller = this.getController();
this.http.action = this.getAction();
return;
}
let paths = pathname.split('/');
let module, controller, action;
if (think.mode === think.mode_module) {
module = paths[0].toLowerCase();
if(this.module){
if(this.module === module){
paths.shift();
}else{
module = this.module;
}
}else if (module && module !== think.dirname.common && think.module.indexOf(module) > -1) {
paths.shift();
}else{
module = '';
}
}
controller = paths.shift();
action = paths.shift();
let module = this.parseModule();
let controller = this.parseController(module);
let paths = http.pathname.split('/');
let action = paths.shift();
this.parseExtPath(paths);
this.http.module = this.getModule(module);
this.http.module = module; //module not need check
this.http.controller = this.getController(controller);
this.http.action = this.getAction(action);
@ -305,7 +342,8 @@ export default class extends think.middleware.base {
if (!controller) {
return think.config('default_controller');
}
if (/^\w+$/.test(controller)) {
//has / in controller
if (/^[\w\/]+$/.test(controller)) {
this.checkLowerCase(controller);
return controller.toLowerCase();
}