Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Routing 正在使用中获取模块路由导入路径_Routing_Module_Playframework - Fatal编程技术网

Routing 正在使用中获取模块路由导入路径

Routing 正在使用中获取模块路由导入路径,routing,module,playframework,Routing,Module,Playframework,如果我的routes文件中有一个用于从模块导入routes的典型条目 * /admin module:crud 除了自己解析主路由文件外,是否有其他方法可以确定crud模块的基本路径是/admin?路由器类似乎没有保存此信息 最终目标是为每个模块创建一个菜单。如果我在/useradmin导入了一个用户管理模块,我想生成一个包含/useradmin/users和/useradmin/groups的菜单,但不包含更深的子体,如/useradmin/us

如果我的routes文件中有一个用于从模块导入routes的典型条目

*       /admin                  module:crud
除了自己解析主路由文件外,是否有其他方法可以确定
crud
模块的基本路径是
/admin
路由器
类似乎没有保存此信息

最终目标是为每个模块创建一个菜单。如果我在
/useradmin
导入了一个用户管理模块,我想生成一个包含
/useradmin/users
/useradmin/groups
的菜单,但不包含更深的子体,如
/useradmin/users/new
。如果
/useradmin
被路由到某个地方,我将使用它作为菜单标签的链接,否则我将只显示纯文本标签


虽然我可能可以在不知道的情况下伪造它,但似乎知道模块的实际基本url是确保我能够处理各种特殊情况的最佳方法,例如在
/modules/useradmin
导入的模块,或者包含孙子路径但没有子路径的模块(
/useradmin/users/new
但没有
/useradmin/users
)。

问题是:为什么需要解析文件或知道“/admin”是基本路径?Play允许您使用来获取路径

从上面的链接:

例如,使用此管线定义:

从代码中,您可以生成能够调用客户端的URL。显示:


这应该足够了。

刚才,我只看到自己解析路由。 查看play.mvc.Router类IMO和以下函数:

static void parse(VirtualFile routeFile, String prefix) {
    String fileAbsolutePath = routeFile.getRealFile().getAbsolutePath();
    int lineNumber = 0;
    String content = routeFile.contentAsString();
    if (content.indexOf("${") > -1 || content.indexOf("#{") > -1 || content.indexOf("%{") > -1) {
        // Mutable map needs to be passed in.
        content = TemplateLoader.load(routeFile).render(new HashMap<String, Object>(16));
    }
    for (String line : content.split("\n")) {
        lineNumber++;
        line = line.trim().replaceAll("\\s+", " ");
        if (line.length() == 0 || line.startsWith("#")) {
            continue;
        }
        Matcher matcher = routePattern.matcher(line);
        if (matcher.matches()) {
            String action = matcher.group("action");
            // module:
            if (action.startsWith("module:")) {
                String moduleName = action.substring("module:".length());
                String newPrefix = prefix + matcher.group("path");
                ...
静态空解析(虚拟文件路由文件,字符串前缀){
字符串fileAbsolutePath=routeFile.getRealFile().getAbsolutePath();
int lineNumber=0;
字符串内容=routeFile.contentAsString();
if(content.indexOf(“${”)>-1 | content.indexOf(“#{”)>-1 | content.indexOf(“%{”)>-1){
//需要传入可变映射。
content=TemplateLoader.load(routeFile.render)(新的HashMap(16));
}
用于(字符串行:content.split(“\n”)){
lineNumber++;
line=line.trim().replaceAll(“\\s+”,”);
if(line.length()==0 | | line.startsWith(“#”){
继续;
}
匹配器匹配器=路由模式匹配器(行);
if(matcher.matches()){
字符串action=matcher.group(“action”);
//模块:
if(action.startsWith(“模块:”){
字符串moduleName=action.substring(“模块:”.length());
字符串newPrefix=前缀+匹配器.group(“路径”);
...

它提取前缀,这是您要查找的部分。

在大多数情况下,您是对的,但我想我有一个想知道的正当理由。我正在开发一个模块,该模块生成您去过的地方的菜单栏和面包屑。目前为模块生成菜单的最佳方法似乎是确定模块“开始”的位置在路径中,然后查找比该路径低一级的可路由路径。@Bemace您能更详细地解释一下要生成的功能吗?我不确定我是否理解它(将此添加到问题中会有所帮助)
map.put("id", 1541);
String url = Router.reverse("Clients.show", map).url;  // GET /clients/1541
static void parse(VirtualFile routeFile, String prefix) {
    String fileAbsolutePath = routeFile.getRealFile().getAbsolutePath();
    int lineNumber = 0;
    String content = routeFile.contentAsString();
    if (content.indexOf("${") > -1 || content.indexOf("#{") > -1 || content.indexOf("%{") > -1) {
        // Mutable map needs to be passed in.
        content = TemplateLoader.load(routeFile).render(new HashMap<String, Object>(16));
    }
    for (String line : content.split("\n")) {
        lineNumber++;
        line = line.trim().replaceAll("\\s+", " ");
        if (line.length() == 0 || line.startsWith("#")) {
            continue;
        }
        Matcher matcher = routePattern.matcher(line);
        if (matcher.matches()) {
            String action = matcher.group("action");
            // module:
            if (action.startsWith("module:")) {
                String moduleName = action.substring("module:".length());
                String newPrefix = prefix + matcher.group("path");
                ...