Question

I'm trying to set up routes in my application such that:

  • /:locale/ -> Home, with locale binding
  • /:locale/search -> Search, with locale binding

Thus far, my routing code is:

(defn controller-routes [locale]
  (home/c-routes locale)
  (search/c-routes locale)))


(defroutes app-routes
  (route/resources "/")
  (context "/:locale" [locale]
    (controller-routes locale))
  no-locale-route
  (route/not-found "Not Found"))

search/c-routes:

(defn c-routes [locale]
 (GET "/search" [] (index locale)))

home/c-routes:

(defn c-routes [locale]
   (GET "/" [] (index locale)))

I can't understand why this doesn't work properly, but currently "/uk/search/" matches correctly, but "/uk/" gives the 404 page.

Any help would be appreciated. Thanks.

Was it helpful?

Solution

controller-routes is a normal function which as of now returns the last route i.e search and hence only search works. What you need is make controller-routes a route using defroutes and changing the c-routes as well:

search/c-routes:

(def c-routes (GET "/search" [locale] (index locale)))

home/c-routes:

(def c-routes (GET "/" [locale] (index locale)))

Where you use above routes:

(defroutes controller-routes
  home/c-routes
  search/c-routes)


(defroutes app-routes
  (route/resources "/")
  (context "/:locale" [locale]
    controller-routes)
  no-locale-route
  (route/not-found "Not Found"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top