Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Nesting routes

timic edited this page · 12 revisions

The context macro provides a way of giving a set of routes a common prefix:

(defroutes user-routes
  (context "/user/current" []
    (GET "/" [] ...) ;the route that exists at "/user/current"
    (GET "/profile" [] ...)
    (GET "/posts" [] ...)))

Route parameters may be added to the context, just like a normal route:

(defroutes user-routes
  (context "/user/:user-id" [user-id]
    (GET "/profile" [] ...)
    (GET "/posts" [] ...)))

Because routes are closures, the user-id symbol is available to use in the two sub routes.

However, if your inner routes are defined separately, you need to manually pass any bound parameters from the context. For example:

(defn inner-routes [user-id]
  (routes
   (GET "/profile" [] ...)
   (GET "/posts" [] ...)))

(defroutes user-routes
  (context "/user/:user-id" [user-id]
    (inner-routes user-id)))

This is because parameters are bound with a lexical, rather than dynamic scope.

Something went wrong with that request. Please try again.