2017-10-29 07:29:06 +00:00
# Router
2018-02-11 17:15:25 +00:00
Routes are just data and to do routing, we need a router instance satisfying the `reitit.core/Router` protocol. Routers are created with `reitit.core/router` function, taking the raw routes and optionally an options map.
2017-10-29 07:29:06 +00:00
2017-10-30 06:46:20 +00:00
The `Router` protocol:
2017-10-29 07:29:06 +00:00
```clj
(defprotocol Router
(router-name [this])
(routes [this])
(options [this])
(route-names [this])
(match-by-path [this path])
(match-by-name [this name] [this name params]))
```
Creating 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-29 07:29:06 +00:00
```
2017-10-30 06:46:20 +00:00
Name of the created router:
```clj
(r/router-name router)
; :mixed-router
```
The flattened route tree:
2017-10-29 07:29:06 +00:00
```clj
(r/routes router)
; [["/api/ping" {:name :user/ping}]
; ["/api/user/:id" {:name :user/user}]]
```
2017-10-30 06:46:20 +00:00
### Behind the scenes
When router is created, the following steps are done:
* route tree is flattened
* route arguments are expanded (via `reitit.core/Expand` protocol) and optionally coerced
* [route conflicts ](advanced/route_conflicts.md ) are resolved
2018-02-11 17:15:25 +00:00
* route tree is compiled
2017-10-30 06:46:20 +00:00
* actual [router implementation ](../advanced/different_routers.md ) is selected and created