2017-12-10 16:08:00 +00:00
|
|
|
# Path-based Routing
|
2017-10-29 07:29:06 +00:00
|
|
|
|
|
|
|
|
Path-based routing is done using the `reitit.core/match-by-path` function. It takes the router and path as arguments and returns one of the following:
|
|
|
|
|
|
|
|
|
|
* `nil`, no match
|
|
|
|
|
* `PartialMatch`, path matched, missing path-parameters (only in reverse-routing)
|
2018-02-11 17:15:25 +00:00
|
|
|
* `Match`, an exact match
|
2017-10-29 07:29:06 +00:00
|
|
|
|
2017-10-30 06:46:20 +00:00
|
|
|
Given a router:
|
|
|
|
|
|
|
|
|
|
```clj
|
|
|
|
|
(require '[reitit.core :as r])
|
|
|
|
|
|
|
|
|
|
(def router
|
|
|
|
|
(r/router
|
2018-02-11 17:15:25 +00:00
|
|
|
["/api"
|
|
|
|
|
["/ping" ::ping]
|
|
|
|
|
["/user/:id" ::user]]))
|
2017-10-30 06:46:20 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
No match returns `nil`:
|
|
|
|
|
|
2017-10-29 07:29:06 +00:00
|
|
|
```clj
|
|
|
|
|
(r/match-by-path router "/hello")
|
|
|
|
|
; nil
|
|
|
|
|
```
|
|
|
|
|
|
2017-10-30 06:46:20 +00:00
|
|
|
Match provides the route information:
|
|
|
|
|
|
2017-10-29 07:29:06 +00:00
|
|
|
```clj
|
|
|
|
|
(r/match-by-path router "/api/user/1")
|
|
|
|
|
; #Match{:template "/api/user/:id"
|
2017-11-18 10:47:16 +00:00
|
|
|
; :data {:name :user/user}
|
2017-10-29 07:29:06 +00:00
|
|
|
; :path "/api/user/1"
|
|
|
|
|
; :result nil
|
2018-02-01 14:23:44 +00:00
|
|
|
; :path-params {:id "1"}}
|
2017-10-29 07:29:06 +00:00
|
|
|
```
|