2016-04-24 52 views
2

我有一個模塊,它有一個控制器和幾個模板。現在,我想引用位於父模塊中的控制器。我想這樣的:如何在Play中使用父模塊中定義的路由?

case Some(user) => Redirect(routes.Application.index) 

不幸的是,我收到以下錯誤:

[error]  case Some(user) => Redirect(routes.Application.index) 
[error]         ^

我試了也是這樣的:

case Some(user) => Redirect(controllers.Application.index) 

但我得到的仍是同樣的錯誤。當我試圖引用作爲模塊一部分的控制器時,也會發生此錯誤。

我試過像這樣:controllers.module.Controller.action

另外,在模塊中引用視圖時,我也遇到了同樣的問題,這些模塊實際上位於父視圖中。

例如,我要引用的模板main模塊中:

@views.html.main 

它拋出一個錯誤。

回答

3

你需要尋找到「集結反向路由器」 - https://www.playframework.com/documentation/2.5.x/AggregatingReverseRouters

例如,我有子模塊「把手」,並希望使用的路線從子模塊中的主要項目。所以,我的下一個字符串添加到`build.sbt「:

lazy val handlebars = (project in file("modules/handlebars")) 
    .enablePlugins(PlayJava) 
    .settings(
    aggregateReverseRoutes := Seq(root) 
    ) 

lazy val root: Project = (project in file(".")) 
    .enablePlugins(PlayJava) 
    .aggregate(handlebars) 
    .dependsOn(handlebars) 

aggregateReverseRoutes:= Seq(root)說SBT首先從root項目編譯路線,並利用它們在編制handlebars項目。由於依賴關係中的遞歸,您還需要爲root項目(如root: Project)使用隱式類型。

問題全文:https://github.com/playframework/playframework/issues/1390

+0

非常感謝您的幫助!你能告訴我如何在我的控制器和模板中引用路線嗎? –

+1

我使用Java,並且我在子項目中的代碼看起來像是'controllers.routes.Assets.versioned(new controllers.Assets.Asset(url))。toString()',其中'url'只是一個字符串。我想,你需要調用'Redirect(controllers.routes.Application.index)'。 –

+0

完美的作品!非常感謝。 –

相關問題