Skip to content
Dru Sellers edited this page Jun 28, 2014 · 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 exits 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.

Clone this wiki locally