Skip to content

Commit af6cb7e

Browse files
readme: updated
1 parent b634e06 commit af6cb7e

File tree

1 file changed

+56
-42
lines changed

1 file changed

+56
-42
lines changed

README.md

+56-42
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,56 @@
22
Kotlin Routing
33
</h1>
44

5-
A multiplatform, extensible, and independent "navigation" routing library powered by Ktor.
5+
A multiplatform, extensible, and independent routing library powered by Ktor.
66
Create routing independently and extend it to what you need.
77

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+
814
```kotlin
915
val router = routing {
1016
route(path = "/login") {
1117
handle {
12-
// ready to go to any screen in any platform that you want
18+
// Handle the call to the routing "/login"
1319
}
1420
}
1521
}
1622

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+
)
1830
```
1931

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+
2039
## Defining routes
2140
> Based on [Ktor Routing](https://ktor.io/docs/routing-in-ktor.html)
2241
2342
```kotlin
2443
val router = routing {
25-
route(path = "/path") {
44+
route(path = "/path") {
45+
// ...
46+
}
2647
}
2748

2849
```
2950
It's also possible to define a name to navigate instead of using the path value.
3051
```Kotlin
3152
route(path = "/path", name = "path") {
53+
// ...
3254
}
33-
router.push("path")
3455
```
3556

3657
[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
4162
val router = routing {
4263
route(path = "/path") {
4364
handle {
65+
// Handle any call to the "/path" route
4466
}
4567
}
4668
}
4769
```
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
4971
```Kotlin
50-
(RouteMethod.Pop, RouteMethod.Push, RouteMethod.Replace, RouteMethod.ReplaceAll)
72+
(StackRouteMethod.Pop, StackRouteMethod.Push, StackRouteMethod.Replace, StackRouteMethod.ReplaceAll)
5173

5274
route(path = "/path2") {
5375
push { } // handle push to this route only
@@ -78,14 +100,14 @@ pop(path = "/path7") {
78100
}
79101
```
80102
### 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
82104

83105
```Kotlin
84106
val application = call.application
85107
val routeMethod = call.routeMethod
86108
val uri = call.uri
87109
val attributes = call.attributes
88-
val parameters = call.parameters
110+
val parameters = call.parameters // routing parameters (see Routing routes) plus query parameters when provided
89111
```
90112

91113
### Redirecting route
@@ -99,9 +121,10 @@ route|handle|push|replace|replaceAll|pop(...) {
99121
}
100122
```
101123
### Regex route
102-
You can also create a route using regex
124+
You can also create a route using regex.
125+
103126
```Kotlin
104-
route|handle|push|replace|replaceAll|pop(path = Regex()) {
127+
route|handle|push|replace|replaceAll|pop(path = Regex(...)) {
105128
// ...
106129
}
107130
```
@@ -129,8 +152,7 @@ router.pushNamed(name = "name", parameters = parametersOf("number", "123"))
129152
// Pushing a name with parameters and path parameters ("/path/{id}")
130153
router.pushNamed(
131154
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
134156
)
135157

136158
// Replacing or replacing all works the same as push but 'replace' instead push :D
@@ -149,6 +171,7 @@ router.pop(parameters = parametersOf("number", "123"))
149171
> Based on [Ktor Type-safe routing](https://ktor.io/docs/type-safe-routing.html)
150172
>
151173
> First you have to put module `resources` as a dependency
174+
> There is a `resources-stack` module with stack behaviors
152175
153176
```kotlin
154177

@@ -169,32 +192,13 @@ class Articles {
169192
val router = routing {
170193
install(Resources)
171194

172-
push<Articles.New> {
173-
// handle push to this route only
195+
handle<Articles> {
196+
// handle any call to Articles
174197
}
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?
185198
}
186199

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())
198202
```
199203

200204
## Exception routing handler
@@ -208,25 +212,35 @@ val router = routing {
208212
install(StatusPages) {
209213
// Catch any exception (change to be specific if you need)
210214
exception<Throwable> { call, cause ->
211-
// exception handled during push, replace or pop routing
215+
// exception handled
212216
}
213217
}
214-
215-
push(path = "/path") {
218+
219+
handle(path = "/path") {
216220
throw IllegalArgumentException("simulating an exception thrown on routing")
217221
}
218222
}
219223

220224
// Pushing to simulate
221-
router.push(path = "/path")
225+
router.execute(
226+
call = MyCustomApplicationCall(
227+
routeMethod = MyCustomRouteMethod(),
228+
uri = "/path"
229+
)
230+
)
222231
```
223232

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+
224238
## Next steps
225239

226-
[ ] - Helper functions for each platform (Android, iOS, Web, Desktop, ...)
240+
[ ] - Helper modules for native platform navigation (Android, iOS, Web, Desktop, ...)
227241

228242
[ ] - Deep Link support by platform
229243

230244
[ ] - More plugins like Session, CallLogging, etc
231245

232-
[ ] - More samples
246+
[ ] - Status-pages redirect support

0 commit comments

Comments
 (0)