mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
reverse-routing docs
This commit is contained in:
parent
1993dee5f2
commit
cb0297ac85
2 changed files with 42 additions and 19 deletions
|
|
@ -81,3 +81,21 @@ There is also a exception throwing version:
|
||||||
(r/match-by-name! router ::user)
|
(r/match-by-name! router ::user)
|
||||||
; ExceptionInfo missing path-params for route /api/user/:id: #{:id}
|
; 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"
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
# Reverse routing with Ring
|
# Reverse routing with Ring
|
||||||
|
|
||||||
Both the `router` and the `match` are injected into Ring Request (as `::r/router` and `::r/match`) by the `reitit.ring/ring-handler` and with that, available to middleware and endpoints.
|
Both the `router` and the `match` are injected into Ring Request (as `::r/router` and `::r/match`) by the `reitit.ring/ring-handler` and with that, available to middleware and endpoints. To convert a `Match` into a path, one can use `r/match->path`, which optionally takes a map of query-parameters too.
|
||||||
|
|
||||||
Below is an example how to use the `router` to do reverse routing from a ring handler:
|
Below is an example how to do reverse routing from a ring handler:
|
||||||
|
|
||||||
```clj
|
```clj
|
||||||
(require '[reitit.core :as r])
|
(require '[reitit.core :as r])
|
||||||
|
|
@ -11,23 +11,28 @@ Below is an example how to use the `router` to do reverse routing from a ring ha
|
||||||
(def app
|
(def app
|
||||||
(ring/ring-handler
|
(ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
[["/users" {:get (fn [{:keys [::r/router]}]
|
[["/users"
|
||||||
|
{:get (fn [{:keys [::r/router]}]
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (for [i (range 10)]
|
:body (for [i (range 10)]
|
||||||
{:uri (:path (r/match-by-name router ::user {:id i}))})})}]
|
{:uri (-> router
|
||||||
["/users/:id" {:name ::user
|
(r/match-by-name ::user {:id i})
|
||||||
|
;; with extra query-params
|
||||||
|
(r/match->path {:iso "möly"}))})})}]
|
||||||
|
["/users/:id"
|
||||||
|
{:name ::user
|
||||||
:get (constantly {:status 200, :body "user..."})}]])))
|
:get (constantly {:status 200, :body "user..."})}]])))
|
||||||
|
|
||||||
(app {:request-method :get, :uri "/users"})
|
(app {:request-method :get, :uri "/users"})
|
||||||
;{:status 200,
|
; {:status 200,
|
||||||
; :body [{:uri "/users/0"}
|
; :body ({:uri "/users/0?iso=m%C3%B6ly"}
|
||||||
; {:uri "/users/1"}
|
; {:uri "/users/1?iso=m%C3%B6ly"}
|
||||||
; {:uri "/users/2"}
|
; {:uri "/users/2?iso=m%C3%B6ly"}
|
||||||
; {:uri "/users/3"}
|
; {:uri "/users/3?iso=m%C3%B6ly"}
|
||||||
; {:uri "/users/4"}
|
; {:uri "/users/4?iso=m%C3%B6ly"}
|
||||||
; {:uri "/users/5"}
|
; {:uri "/users/5?iso=m%C3%B6ly"}
|
||||||
; {:uri "/users/6"}
|
; {:uri "/users/6?iso=m%C3%B6ly"}
|
||||||
; {:uri "/users/7"}
|
; {:uri "/users/7?iso=m%C3%B6ly"}
|
||||||
; {:uri "/users/8"}
|
; {:uri "/users/8?iso=m%C3%B6ly"}
|
||||||
; {:uri "/users/9"}]}
|
; {:uri "/users/9?iso=m%C3%B6ly"})}
|
||||||
```
|
```
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue