2011-03-22 124 views
1

如果我在我的路線文件從一個模塊獲取模塊路由引入路徑

*  /admin     module:crud 

引入路由典型的條目是有我確定爲crud模塊的基本路徑是一種/admin,除了自己解析主要路徑文件?看起來Router類沒有保存這些信息。

最終目標是爲每個模塊創建一個菜單。如果我有一個導入/useradmin的用戶管理模塊,我想要生成一個菜單,其中包含/useradmin/users/useradmin/groups,但不包括更深的後代,如/useradmin/users/new。如果/useradmin被路由到某個東西,我將使用它作爲菜單標籤的鏈接,否則我只會顯示一個純文本標籤。

儘管我可能在不知情的情況下僞造它,但似乎知道該模塊的實際基址是確保我可以處理各種特殊情況的最佳方法,例如導入的模塊爲/modules/useradmin或模塊爲孫子路徑但沒有孩子的路徑(/useradmin/users/new但沒有/useradmin/users)。

回答

2

問題是:爲什麼你需要解析文件或知道'/ admin'是基地? Play允許您使用reverse routing獲取路徑。

從上面的鏈接:

例如,使用這條路線的定義:

GET /clients/{id}  Clients.show 

從您的代碼,您可以生成能夠調用Clients.show的網址:

map.put("id", 1541); 
String url = Router.reverse("Clients.show", map).url; // GET /clients/1541 

這應該夠了。

+0

對於大多數情況你是對的,但我認爲我有一個想知道的合法理由。我正在研究一個模塊,它可以生成你去過的地方的菜單和麪包屑。目前,爲模塊生成菜單的最佳方法似乎是確定模塊在路徑中「啓動」的位置,然後查找比該路徑低一級的可路由路徑。 – 2011-03-22 16:32:12

+0

@Bemace您能否更詳細地解釋您想要生成的功能?我不確定我是否理解它(將此添加到問題中會有所幫助) – 2011-03-23 10:06:07

1

就在現在,我沒有看到任何事情自己解析路線。 看那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"); 
       ... 

它提取前綴這是您要查找的一部分。