Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse routing / URL generation in Clojure?

What are some good ways (libraries or code) to do both routing and URL generation in Clojure?

I looked at the Compojure source code, but saw no indication that its routing is meant to also serve for URL generation. What else is out there?

An answer that says "there is no such library" is actually very useful, if you back it up with evidence and/or experience.

The motivation for this question comes from the first section of the Rails Routing Guide: "The Rails router recognizes URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views."

Dear You: I know someone out there (maybe you?) wants to say, "don't worry about URL generation; just hardcode strings for your URL's". Yes, I know I could do that, but that does not constitute an answer to this question. :) This is not a question about the wisdom or pros and cons of combined routing and URL generation. It is about how do you do it?

like image 805
David J. Avatar asked May 28 '13 00:05

David J.


3 Answers

Pedestal's service layer does both routing and URL generation; this functionality is described in the Service Routing section of Pedestal documentation.

like image 159
Michał Marczyk Avatar answered Nov 16 '22 02:11

Michał Marczyk


I just came across route-one which looks like it does just this. It's a small library which is fully compatible with Compojure, and provides a way of defining routes which can be run in reverse.

From their documentation, you can define a route like this:

(defroute document "/documents/:document-id")

And a couple of helper functions will be generated:

(document-path :document-id "123")
;; => "/documents/123"

(with-base-url "https://myservice.com"
   (document-url :document-id "123"))
;; => "https://myservice.com/documents/123"
like image 45
Daniel Neal Avatar answered Nov 16 '22 01:11

Daniel Neal


I've written a minimal replacement for compojure's routing macros that allows you to name routing patterns and generate urls for them. It's at https://github.com/joodie/clout-link

like image 1
Joost Diepenmaat Avatar answered Nov 16 '22 00:11

Joost Diepenmaat