2015-02-05 61 views
5

我想要将多个子域和/或根域指向单个Play Framework 2.3(Scala)应用程序,例如apples.com,bananas.com或buy.bananas.com。Play Framework 2.3中的域路由

根据请求来自哪个域,我想拥有不同的路由。

理想的情况下,它应该工作是这样的:

GET apples.com  @controllers.ApplesController.home 
GET bananas.com  @controllers.BananasController.home 
GET buy.bananas.com @controllers.BananasController.buy 

有没有办法在游戏框架2.3做到这一点?

+5

像这样的事情更好地由http服务器处理...不是由框架。不过,你可以实现一些这样的功能。有关更多详情,请参阅 - > http://typesafe.com/activator/template/play-multidomain-seed和https://github.com/adrianhurt/play-multidomain-seed/blob/master/app/Global.scala – 2015-02-05 20:26:32

+1

@SarveshKumarSingh写一个答案而不是评论,这是点赞/接受 – biesior 2015-02-05 20:31:20

+0

@biesior解决方案的范围不适合答案。需要了解的东西太多,需要以特定的方式创建整个项目。 – 2015-02-05 20:34:08

回答

5

我在java中。这里的工作是做它在Java中的方法也许能够帮助

路线

GET /   @controllers.ApplesController.index 
GET /apples  @controllers.ApplesController.home 
GET /bananas  @controllers.BananasController.home 
GET /buybananas @controllers.BananasController.buy 

控制器

@With(CheckUrl.class) 
public static Result index() { 
     return ok(index.render("Unable to resolve host.")); 
    } 

CheckUrl.java

public class CheckUrl extends play.mvc.Action.Simple { 

    public F.Promise<SimpleResult> call(Http.Context ctx) throws Throwable { 

     String host = request().host(); 
     System.out.println("HOST IS "+host); 

       if (host.equalsIgnoreCase("apples.com")) { 

      return F.Promise.pure(redirect("/apples")); 

     }else if (host.equalsIgnoreCase("bananas.com ")){ 

     return F.Promise.pure(redirect("/bananas")); 

     }else if (host.equalsIgnoreCase("buy.bananas.com")){ 

     return F.Promise.pure(redirect("/buybananas")); 
     }else{ 
      return delegate.call(ctx); 
     } 



} 

I不知道它是否是这样做的好方法。我已经用request().uri()试过了,但没有与request().host()试过,这对我很有帮助。可能会有所帮助。

+1

这是我的问题的解决方案,谢谢。请确保也签出github链接发布的其他人作为我的问题的评论。在我看来,它实际上是一种更好更清洁的方式,所以也可能对您有所帮助。 – 2015-02-07 01:52:52