reitit/doc/basics/path_based_routing.md

38 lines
768 B
Markdown
Raw Normal View History

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)
* `Match`, exact match
Given a router:
```clj
(require '[reitit.core :as r])
(def router
(r/router
[["/api"
["/ping" ::ping]
["/user/:id" ::user]]]))
```
No match returns `nil`:
2017-10-29 07:29:06 +00:00
```clj
(r/match-by-path router "/hello")
; nil
```
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
```