Viet Anh
Viet Anh

Reputation: 165

How to get referrer url in playframework

I want to redirect back to referrer url in playframework and I have tried to log this url after request by log function in controller:

@After
public static void log(){
    session.put("referrer", request.url);
}

It works but I don't want to write the same code in every controller. When I let all the controllers extend AbstractController class(which extends play.Controller) and put this code in to AbstracController, I received this error:

Unexpected error : Cannot read parameter names for public static void controllers.AbstractController.log()

Can anyone help me. Thanks in advance.

Upvotes: 1

Views: 4849

Answers (2)

palako
palako

Reputation: 3470

request.headers.get("referer");

Upvotes: 1

emt14
emt14

Reputation: 4896

try adding this method in a utility object and annotate your controllers with @With

public class ControllerUtil extends Controller{
    @After
    public static void log(){
        session.put("referrer", request.url);
    }
}

@With(ControlerUtil.class)
public MyController extends Controller{
   public static index(){
      render();
   }
}

Alternatively annotate your log methods with @Util

UPDATE

Correct, you need to have your ControllerUtil extend Controller. If you prefer name it Application instead of ControllerUtil

Upvotes: 4

Related Questions