The context macro provides a way of giving a set of routes a common prefix:
context
(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.
user-id
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.
There was an error while loading. Please reload this page.