mirror of
https://github.com/metosin/reitit.git
synced 2026-02-07 20:33:12 +00:00
Some checks failed
testsuite / Clojure 11 (Java 11) (push) Has been cancelled
testsuite / Clojure 11 (Java 17) (push) Has been cancelled
testsuite / Clojure 11 (Java 21) (push) Has been cancelled
testsuite / Clojure 11 (Java 25) (push) Has been cancelled
testsuite / Clojure 12 (Java 11) (push) Has been cancelled
testsuite / Clojure 12 (Java 17) (push) Has been cancelled
testsuite / Clojure 12 (Java 21) (push) Has been cancelled
testsuite / Clojure 12 (Java 25) (push) Has been cancelled
testsuite / ClojureScript (push) Has been cancelled
testsuite / Lint cljdoc.edn (push) Has been cancelled
testsuite / Check cljdoc analysis (push) Has been cancelled
for #778 #519
112 lines
2.5 KiB
Markdown
112 lines
2.5 KiB
Markdown
# Name-based (reverse) Routing
|
|
|
|
All routes which have `:name` route data defined can also be matched by name.
|
|
|
|
Given a router:
|
|
|
|
```clj
|
|
(require '[reitit.core :as r])
|
|
|
|
(def router
|
|
(r/router
|
|
["/api"
|
|
["/ping" ::ping]
|
|
["/user/:id" ::user]]))
|
|
```
|
|
|
|
Listing all route names:
|
|
|
|
```clj
|
|
(r/route-names router)
|
|
; [:user/ping :user/user]
|
|
```
|
|
|
|
No match returns `nil`:
|
|
|
|
```clj
|
|
(r/match-by-name router ::kikka)
|
|
nil
|
|
```
|
|
|
|
Matching a route:
|
|
|
|
```clj
|
|
(r/match-by-name router ::ping)
|
|
; #Match{:template "/api/ping"
|
|
; :data {:name :user/ping}
|
|
; :result nil
|
|
; :path-params {}
|
|
; :path "/api/ping"}
|
|
```
|
|
|
|
If not all path-parameters are set, a `PartialMatch` is returned:
|
|
|
|
```clj
|
|
(r/match-by-name router ::user)
|
|
; #PartialMatch{:template "/api/user/:id",
|
|
; :data {:name :user/user},
|
|
; :result nil,
|
|
; :path-params nil,
|
|
; :required #{:id}}
|
|
|
|
(r/partial-match? (r/match-by-name router ::user))
|
|
; true
|
|
```
|
|
|
|
With provided path-parameters:
|
|
|
|
```clj
|
|
(r/match-by-name router ::user {:id "1"})
|
|
; #Match{:template "/api/user/:id"
|
|
; :data {:name :user/user}
|
|
; :path "/api/user/1"
|
|
; :result nil
|
|
; :path-params {:id "1"}}
|
|
```
|
|
|
|
Path-parameters are automatically coerced into strings, with the help of (currently internal) Protocol `reitit.impl/IntoString`. It supports strings, numbers, booleans, keywords and objects:
|
|
|
|
```clj
|
|
(r/match-by-name router ::user {:id 1})
|
|
; #Match{:template "/api/user/:id"
|
|
; :data {:name :user/user}
|
|
; :path "/api/user/1"
|
|
; :result nil
|
|
; :path-params {:id "1"}}
|
|
```
|
|
|
|
In case you want to do something like generate a template path for documentation, you can disable url-encoding:
|
|
|
|
```clj
|
|
(r/match-by-name router ::user {:id "<id goes here>"} {:url-encode? false})
|
|
; #reitit.core.Match{:template "/api/user/:id"
|
|
; :data {:name :user/user}
|
|
; :path "/api/user/<id goes here>"
|
|
; :result nil
|
|
; :path-params {:id "<id goes here>"}}
|
|
```
|
|
|
|
There is also an exception throwing version:
|
|
|
|
```clj
|
|
(r/match-by-name! router ::user)
|
|
; ExceptionInfo missing path-params for route /api/user/:id: #{:id}
|
|
```
|
|
|
|
To turn a Match into a path, there is `reitit.core/match->path`:
|
|
|
|
```clj
|
|
(-> router
|
|
(r/match-by-name ::user {:id 1})
|
|
(r/match->path))
|
|
; "/api/user/1"
|
|
```
|
|
|
|
It can take an optional map of query-parameters too:
|
|
|
|
```clj
|
|
(-> router
|
|
(r/match-by-name ::user {:id 1})
|
|
(r/match->path {:iso "möly"}))
|
|
; "/api/user/1?iso=m%C3%B6ly"
|
|
```
|