Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain routes in Play Framework 2.3

I want to point multiple subdomains and/or root domains to a single Play Framework 2.3 (Scala) application, for example apples.com, bananas.com or buy.bananas.com.

Depending on which domain the request comes in, I want to have different routes.

Ideally, it should work something like this:

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

Is there any way to do this in Play Framework 2.3?

like image 879
Daniel Lang Avatar asked Feb 05 '15 20:02

Daniel Lang


1 Answers

I am working in java .Here is the way to do it in java maybe that could help

routes

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

controller

@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 dont know if it the good way of doing it.I have tried it with request().uri() but not with request().host() and that worked for me.Maybe that could help.

like image 55
singhakash Avatar answered Sep 29 '22 14:09

singhakash