Get the "path" from the request

Hi everyone,
I am trying to programatically exclude a section of the MAIN (template) from pages in my application, depending on their path.

I was hoping to have something like the following in my MAIN file;
@if(request.path !="/"){ ... }

So that I can maintain the menu in one place (MAIN) for the entire application.

I have added (implicit request: Request) to my MAIN and INDEX pages (and XOR) individually.

Also; the controller for the index page is;

def index: Action[AnyContent] = Action {implicit request =>
        Ok(au.com.ecpr.pas.views.html.index())
}

With Implicit declared in both I get;
trait Request takes type parameters

With it JUST declared in the INDEX page I get;
object path is not a member of package play.api.mvc.request

I have also tried with;
(implicit request : RequestHeader) :- but get;
Cannot find any HTTP Request Header here

I have absolutely no idea what I am doing wrong - and would love some help please.
Lastly I could see it being a good addition to the documentation - retrieving data about the request would seems to be a common use-case?

Thanks.

OK I have managed to work it out… I was doing a few things wrong;

  • had to use RequestHeader not Request

  • had to add the (implicit request : RequestHeader) to the index page, so the test,
    @if(request.path!="/")
    could be tested and into the main.scala.html, too.

  • Also - (implicit declarations) cannot be the only parameter list passed into a view
    If you don’t have any other parameters to pass into the view you must supply an empty parameter list, first, E.g.;
    @()(implicit request : RequestHeader)

Then you can use @request.path / uri / queryString / etc. in your twirl template.

1 Like