Put this in your Boot.scala file inside the def Boot method:
val idRewriter: LiftRules.RewritePF = {
case RewriteRequest(path @ ParsePath("user" :: id :: _, _, _,_),
_, _) => RewriteResponse(
ParsePath("user" :: "index" :: Nil, "", true, false),
Map("id" -> id :: path.wholePath.drop(2)
.zipWithIndex.map(p => ("param"+(p._2 + 1)) -> p._1) :_*)
)
}
LiftRules.rewrite.prepend(idRewriter)
You can call it anything. Here it is called idRewriter.
The val "idRewriter" is set to a LiftRules Rewrite partial function. The
case keyword matches the Req against the provided RewriteRequest. The
first parameter is a ParsePath. "path @" means you are matching against
a piece of this ParsePath.
Here we match against /user/ with the first item in the list, "user."
Whatever comes after that is placed in the id variable. We don't care
what comes after that, so the last item in the list is _.
The last three parts of the ParsePath are unimportant, thus the _, _, _.
But if we wanted to, the first of those represents a suffix, so we could do:
path @ ParsePath("user" :: id :: _, "html", _, _)
if we wanted to match only against files with the extension "html." The
next parameter is a Boolean indicating whether the path is an absolute
path. The final parameter is a Boolean indicating whether an end slash
is required.
So if I've understood it correctly, then:
path @ ParsePath("user" :: id :: _, "html", false, false)
should only match against a file at user/<id> that ends in .html. Note
that there is no leading slash (the path is relative) and there is no
ending slash.
The remaining parameters in the RewriteRequest are the RequestType (e.g.
GetRequest) and the HttpServletRequest itself. For example:
RewriteRequest(path @ ParsePath("user" :: id :: _, _, _, _),
PostRequest, _)
limits the matches to "post" requests.
When a matching request comes in, this partial function then writes a
RewriteResponse which sends the request to the List("user", "index",
Nil) path (/user/index.html), and passes the id in as a parameter named
"id" -- so you can access it via S.param("id").openOr("") (that's what
the Map part does). The "wholePath.drop(2)" part drops the "user/index"
part. If you were matching against three directories (e.g.
/admin/user/123), you'd drop(3).
Does this help?
Chas.
harryh wrote:
> I want the URLs for user profile pages on my site to be in the
> following form:
>
> http://mydomain.com/user/123
>
> Where 123 is the user id. I'm having trouble figuring out how I can
> add pages like this where the URL path does not correspond to the path
> under webapp/
>
> Help? This can't be very hard I'm sure.
>
> -harryh, just getting started with lift
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---