api7 / lua-resty-radixtree

Adaptive Radix Trees implemented in Lua / LuaJIT
https://api7.ai/
Apache License 2.0
256 stars 61 forks source link

Q:if use the Parameters in path , how to get path param in handler #125

Closed shijunLee closed 1 year ago

shijunLee commented 1 year ago

I want to get path parameters in apisix control api , the route default call dispatch method ,but can not get path param in the customer control api

datavisorchengpeng commented 1 year ago

Found a way, not very official though:

local radix = require "resty.radixtree"
local rx = radix.new({
    {
        paths = { "/name/:name/*other" },
        methods = { "GET" },
        metadata = "metadata /bb",
        handler = function(opts)
            local matched = opts.matched
            ngx.say(matched.name)
            ngx.say(matched.other)
        end
    }
})

assert(rx)

local path = string.gsub(ngx.var.uri, "?.*", "")

local opts = {
    matched = {},
    method = ngx.req.get_method(),
}
local ok = rx:dispatch(path, opts, opts)
if not ok then
    ngx.log(ngx.ERR, "router dispath failed")
    ngx.exit(ngx.HTTP_NOT_FOUND)
end
shijunLee commented 1 year ago

Found a way, not very official though:

local radix = require "resty.radixtree"
local rx = radix.new({
    {
        paths = { "/name/:name/*other" },
        methods = { "GET" },
        metadata = "metadata /bb",
        handler = function(opts)
            local matched = opts.matched
            ngx.say(matched.name)
            ngx.say(matched.other)
        end
    }
})

assert(rx)

local path = string.gsub(ngx.var.uri, "?.*", "")

local opts = {
    matched = {},
    method = ngx.req.get_method(),
}
local ok = rx:dispatch(path, opts, opts)
if not ok then
    ngx.log(ngx.ERR, "router dispath failed")
    ngx.exit(ngx.HTTP_NOT_FOUND)
end

yes, i current use this method get the request param,thanks