Vapor 4 - 创建一条列出所有可用路线的路线
Vapor 4 - Create a route that lists all available routes
我想在 127.0.0.1:8080/help/routes 上创建一条路线,列出所有可用路线(如帮助页面)。我怎样才能做到这一点?谢谢
您可以通过打印 app.routes.all
的内容来实现
像这样编辑您的 routes.swift
文件
import Vapor
func routes(_ app: Application) throws {
app.get("help", "routes") { req -> String in
app.routes.all.description
}
}
您应该按照@imike 的回答使用routes.all。
首先,从请求中获取路由数组:
import Vapor
func help(_ req: Request) -> EventLoopFuture<View> {
struct Context: Codable {
var routes: [Route]
}
let routes: [Route] = req.application.routes.all
let context = Context(routes: routes)
return req.view.render("help", context)
}
然后,在您的 help.leaf 页面中迭代该路线:
<ol>
#for(route in routes):
<li>
#(route.description)
</li>
#endfor
</ol>
Route有显示方法类型、路径等属性
顺便说一句(我知道这不是您问题的严格答案,但了解它可能对其他人有用),您可以直接从 vapor (Vapor 4) 获取此信息:
在终端中转到项目的根目录:
run one of following commands:
‘vapor run routes’ or ‘swift run Run routes’
(Depends whether you have Vapor Toolbox installed or not.)
+-----+------------+
| GET | / |
+-----+------------+
| GET | /hello |
+-----+------------+
| GET | /about/all |
+-----+------------+
我想在 127.0.0.1:8080/help/routes 上创建一条路线,列出所有可用路线(如帮助页面)。我怎样才能做到这一点?谢谢
您可以通过打印 app.routes.all
像这样编辑您的 routes.swift
文件
import Vapor
func routes(_ app: Application) throws {
app.get("help", "routes") { req -> String in
app.routes.all.description
}
}
您应该按照@imike 的回答使用routes.all。
首先,从请求中获取路由数组:
import Vapor
func help(_ req: Request) -> EventLoopFuture<View> {
struct Context: Codable {
var routes: [Route]
}
let routes: [Route] = req.application.routes.all
let context = Context(routes: routes)
return req.view.render("help", context)
}
然后,在您的 help.leaf 页面中迭代该路线:
<ol>
#for(route in routes):
<li>
#(route.description)
</li>
#endfor
</ol>
Route有显示方法类型、路径等属性
顺便说一句(我知道这不是您问题的严格答案,但了解它可能对其他人有用),您可以直接从 vapor (Vapor 4) 获取此信息:
在终端中转到项目的根目录:
run one of following commands: ‘vapor run routes’ or ‘swift run Run routes’ (Depends whether you have Vapor Toolbox installed or not.) +-----+------------+ | GET | / | +-----+------------+ | GET | /hello | +-----+------------+ | GET | /about/all | +-----+------------+