2
2
Kotlin Routing
3
3
</h1 >
4
4
5
- A multiplatform, extensible, and independent "navigation" routing library powered by Ktor.
5
+ A multiplatform, extensible, and independent routing library powered by Ktor.
6
6
Create routing independently and extend it to what you need.
7
7
8
+ ## Core module
9
+
10
+ The core module is the Ktor routing engine modified to be "server" and "client".
11
+ It is abstract and ready to extend.
12
+ Using core module you can:
13
+
8
14
``` kotlin
9
15
val router = routing {
10
16
route(path = " /login" ) {
11
17
handle {
12
- // ready to go to any screen in any platform that you want
18
+ // Handle the call to the routing "/login"
13
19
}
14
20
}
15
21
}
16
22
17
- router.push(path = " /login" )
23
+ // And to call login routing...
24
+ router.execute(
25
+ call = MyCustomApplicationCall (
26
+ routeMethod = MyCustomRouteMethod (),
27
+ uri = " /login"
28
+ )
29
+ )
18
30
```
19
31
32
+ ## Core-stack module
33
+
34
+ This module is a core extension providing Stack based behaviors (push, pop, replace, ...)
35
+ So, if you need stack behaviors and avoid creating your custom ApplicationCall? It is for you.
36
+
37
+ > Keep reading to see how use stack routing
38
+
20
39
## Defining routes
21
40
> Based on [ Ktor Routing] ( https://ktor.io/docs/routing-in-ktor.html )
22
41
23
42
``` kotlin
24
43
val router = routing {
25
- route(path = " /path" ) {
44
+ route(path = " /path" ) {
45
+ // ...
46
+ }
26
47
}
27
48
28
49
```
29
50
It's also possible to define a name to navigate instead of using the path value.
30
51
``` Kotlin
31
52
route(path = " /path" , name = " path" ) {
53
+ // ...
32
54
}
33
- router.push("path")
34
55
```
35
56
36
57
[ Type-safe] ( https://github.com/programadorthi/kotlin-routing/edit/main/README.md#type-safe-routing ) navigation is also supported.
@@ -41,13 +62,14 @@ Since you defined your routes, it's time to handle them. Use the `handle` block
41
62
val router = routing {
42
63
route(path = " /path" ) {
43
64
handle {
65
+ // Handle any call to the "/path" route
44
66
}
45
67
}
46
68
}
47
69
```
48
- It ' s possible to define an action for each RouteMethod available
70
+ Using core-stack module, it 's possible to define an action for each StackRouteMethod available
49
71
``` Kotlin
50
- (RouteMethod .Pop, RouteMethod .Push, RouteMethod .Replace, RouteMethod .ReplaceAll)
72
+ (StackRouteMethod .Pop , StackRouteMethod .Push , StackRouteMethod .Replace , StackRouteMethod .ReplaceAll )
51
73
52
74
route(path = " /path2" ) {
53
75
push { } // handle push to this route only
@@ -78,14 +100,14 @@ pop(path = "/path7") {
78
100
}
79
101
```
80
102
### Getting route detail
81
- Use `call` inside of handle block or any `RouteMethod` to get all details available of a route that was called
103
+ Use ` call ` inside of handle block or any ` StackRouteMethod ` (core-stack) to get all details available of a route that was called
82
104
83
105
``` Kotlin
84
106
val application = call.application
85
107
val routeMethod = call.routeMethod
86
108
val uri = call.uri
87
109
val attributes = call.attributes
88
- val parameters = call.parameters
110
+ val parameters = call.parameters // routing parameters (see Routing routes) plus query parameters when provided
89
111
```
90
112
91
113
### Redirecting route
@@ -99,9 +121,10 @@ route|handle|push|replace|replaceAll|pop(...) {
99
121
}
100
122
```
101
123
### Regex route
102
- You can also create a route using regex
124
+ You can also create a route using regex.
125
+
103
126
``` Kotlin
104
- route|handle|push|replace|replaceAll|pop(path = Regex()) {
127
+ route|handle|push|replace|replaceAll|pop(path = Regex (.. . )) {
105
128
// ...
106
129
}
107
130
```
@@ -129,8 +152,7 @@ router.pushNamed(name = "name", parameters = parametersOf("number", "123"))
129
152
// Pushing a name with parameters and path parameters ("/path/{id}")
130
153
router.pushNamed(
131
154
name = " name" ,
132
- pathParameters = parametersOf("id", "456"), // It will be used to do a replace in the path
133
- parameters = parametersOf("number", "123"),
155
+ parameters = parametersOf(" id" , " 456" ), // It will be used to replace {id} path parameter
134
156
)
135
157
136
158
// Replacing or replacing all works the same as push but 'replace' instead push :D
@@ -149,6 +171,7 @@ router.pop(parameters = parametersOf("number", "123"))
149
171
> Based on [ Ktor Type-safe routing] ( https://ktor.io/docs/type-safe-routing.html )
150
172
>
151
173
> First you have to put module ` resources ` as a dependency
174
+ > There is a ` resources-stack ` module with stack behaviors
152
175
153
176
``` kotlin
154
177
@@ -169,32 +192,13 @@ class Articles {
169
192
val router = routing {
170
193
install(Resources )
171
194
172
- push <Articles.New > {
173
- // handle push to this route only
195
+ handle <Articles > {
196
+ // handle any call to Articles
174
197
}
175
-
176
- replace<Articles> {
177
- // handle replace to this route only
178
- }
179
-
180
- replaceAll<Articles> {
181
- // handle replaceAll to this route only
182
- }
183
-
184
- // There is no use case for type-safe pop handler. Maybe in the future?
185
198
}
186
199
187
- // Pushing a typed route
188
- router.push(Articles.New())
189
-
190
- // Replacing a typed route
191
- router.replace(Articles())
192
-
193
- // Pushing or Replacing a typed route with parameters
194
- router.push|replace|replaceAll(Articles.Id(id = 123))
195
-
196
- // Popping the last pushed or replaced route
197
- router.pop()
200
+ // And do:
201
+ router.execute(Articles ())
198
202
```
199
203
200
204
## Exception routing handler
@@ -208,25 +212,35 @@ val router = routing {
208
212
install(StatusPages ) {
209
213
// Catch any exception (change to be specific if you need)
210
214
exception<Throwable > { call, cause ->
211
- // exception handled during push, replace or pop routing
215
+ // exception handled
212
216
}
213
217
}
214
-
215
- push (path = "/path") {
218
+
219
+ handle (path = " /path" ) {
216
220
throw IllegalArgumentException (" simulating an exception thrown on routing" )
217
221
}
218
222
}
219
223
220
224
// Pushing to simulate
221
- router.push(path = "/path")
225
+ router.execute(
226
+ call = MyCustomApplicationCall (
227
+ routeMethod = MyCustomRouteMethod (),
228
+ uri = " /path"
229
+ )
230
+ )
222
231
```
223
232
233
+ ## Voyager module
234
+
235
+ A [ Voyager] ( https://github.com/adrielcafe/voyager/ ) extension to do navigation using routes.
236
+ See unit tests for more details.
237
+
224
238
## Next steps
225
239
226
- [ ] - Helper functions for each platform (Android, iOS, Web, Desktop, ...)
240
+ [ ] - Helper modules for native platform navigation (Android, iOS, Web, Desktop, ...)
227
241
228
242
[ ] - Deep Link support by platform
229
243
230
244
[ ] - More plugins like Session, CallLogging, etc
231
245
232
- [ ] - More samples
246
+ [ ] - Status-pages redirect support
0 commit comments