request mappings - elvinzeng/nwf GitHub Wiki
general principle
The request mapping rule of nwf also adhere to the principle of convention over configuration like .NET.
specific rules
path | function | example path | example function |
---|---|---|---|
/ | www/controller/RootController.index(ctx); | - | - |
/xxx | www/controller/RootController.xxx(ctx); | /about | www/controller/RootController.about(ctx); |
/xxx/ | www/controller/XxxController.index(ctx); | /demo/ | www/controller/DemoController.index(ctx); |
/xxx/aaa | www/controller/XxxController.aaa(ctx); | /demo/sayHello | www/controller/DemoController.sayHello(ctx); |
/aaa/bbb/ccc | www/controller/aaa/BbbController.ccc(ctx); | /sys/config/update | www/controller/sys/ConfigController.update(ctx); |
you can nested any layers of directory, framework will use the last two section of path to search function.
explicit register request mappings
API:
-- register request mapping
-- @param requestPath: request path
-- @param controllerFunc: function of controller
-- @param validatorFunc: function of validator
nwf.registerRequestMapping(requestPath, controllerFunc, validatorFunc);
-- register controller
-- @param requestPath: request path
-- @param controllerFunc: function of controller
nwf.registerController(requestPath, controllerFunc);
-- register validator
-- @param requestPath: request path
-- @param validatorFunc: function of validator
nwf.registerValidator(requestPath, validatorFunc)
e.g.
nwf.registerRequestMapping("/aaa/bbb/ccc/ddd", function(ctx)
return "test", {message = "Hello, Elvin!"};
end, function(params)
-- do validation here
-- return validation result here;
end);
nwf.registerController("/test/a-b/c-d", function()
return {message="hello elvin!"};
end);
nwf.registerValidator("/test/a-b/c-d", function()
return true;
end);
explicit register request mappings in controller or validator file
By default nwf will load validator and controller file until first request comming. So, it is impossible to explicit register validator or controller in your validator or controller file by default.
If you want to explicit register validator or controller in your validatoror controller file, you need to install module "preload_controller_mod".
$ ./nwf_module_manage.sh -i preload_controller_mod
This module will search all controller and validator and preload it at website startup.