You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/building-projects/routing.md
+54-1Lines changed: 54 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,13 @@
2
2
3
3
## Loaders
4
4
5
-
Routing is a big part of any web library, and there are many ways to do it. View does it's best to support as many methods as possible to give you a well-rounded approach to routing. In view, your choice of routing is called the loader/loader strategy, and there are four of them:
5
+
Routing is a big part of any web library, and there are many ways to do it. View does it's best to support as many methods as possible to give you a well-rounded approach to routing. In view, your choice of routing is called the loader/loader strategy, and there are five of them:
6
6
7
7
-`manual`
8
8
-`simple`
9
9
-`filesystem`
10
10
-`patterns`
11
+
-`custom`
11
12
12
13
## Manually Routing
13
14
@@ -198,10 +199,62 @@ def index():
198
199
return"Hello, view.py!"
199
200
```
200
201
202
+
## Custom Routing
203
+
204
+
The `custom` loader is, you guessed it, a user-defined loader. To start, decorate a function with `custom_loader`:
As shown above, there are two parameters to the `custom_loader` callback:
221
+
222
+
- The `App` instance.
223
+
- The `Path` set by the `loader_path` config setting.
224
+
225
+
The `custom_loader` callback is expected to return a list (or any iterable) of collected routes.
226
+
227
+
!!! tip "Don't reimplement router functions!"
228
+
229
+
You might be confused about the `Route` constructor. That's because it's undocumented, and still technically a private API (meaning it can change at any time, for no reason). Don't try and instantiate a route yourself! Instead, let router functions do it (e.g. `get` or `query`), and collect the functions (or really, `Route` instances)
230
+
231
+
For example, if you wanted to implement a loader that added one route:
# Disregarding the app and path here! Don't do that!
243
+
@get("/my_route")
244
+
defmy_route():
245
+
return"Hello from my loader!"
246
+
247
+
return [my_route]
248
+
249
+
app.run()
250
+
```
251
+
201
252
## Review
202
253
203
254
In view, a loader is defined as the method of routing used. There are three loaders in view.py: `manual`, `simple`, and `filesystem`.
204
255
205
256
-`manual` is good for small projects that are similar to Python libraries like [Flask](https://flask.palletsprojects.com/en/3.0.x/) or [FastAPI](https://fastapi.tiangolo.com).
206
257
-`simple` routing is the recommended loader for full-scale view.py applications.
207
258
-`filesystem` routing is similar to how JavaScript frameworks like [NextJS](https://nextjs.org) handle routing.
259
+
-`patterns` is similar to [Django](https://djangoproject.com/) routing.
260
+
-`custom` let's you decide - you can make your own loader and figure it out as you please.
0 commit comments