webman的route()方法生成url,get参数的问题

小天天天天

正在学习webman,我想生成一个带不确定数量get参数的url

//定义了路由
Route::add(['GET','POST'],'/edit.html',[\app\controller\Admin\ArticleController::class,'edit'])->name('Admin:Article@edit');

我使用

route('Admin:Article@edit',['id'=>8,'uid'=>1,.......])
//生成的url是/edit.html没有携带get参数

我希望生成/edit.html?id=8&uid=1&from=admin.....(很多不确定的get参数),应该如何处理?

萌新一枚,希望各位大佬解答一下

1329 2 1
2个回答

Tinywan

get参数和路由没关系

小天天天天

我尝试修改了Route.php的url方法:

/**
     * @param $parameters
     * @return string
     */
    public function url($parameters = [])
    {
        if (empty($parameters)) {
            return $this->_path;
        }
        $path = str_replace(['[', ']'], '', $this->_path);
        $path = preg_replace_callback('/\{(.*?)(?:\:[^\}]*?)*?\}/', function ($matches) use (&$parameters) {
            if (!$parameters) {
                return $matches[0];
            }
            if (isset($parameters[$matches[1]])) {
                $value = $parameters[$matches[1]];
                unset($parameters[$matches[1]]);
                return $value;
            }
            $key = key($parameters);
            if (is_int($key)) {
                $value = $parameters[$key];
                unset($parameters[$key]);
                return $value;
            }
            return $matches[0];
        }, $path);

        return count($parameters)>0?$path . '?' . http_build_query($parameters) : $path;

    }

现在可以将匹配后剩余的参数以?的形式缀在route()函数生成的URL后面,但是我不确定这种方法有没有其他后遗症

  • walkor 2022-05-03

    看起来没问题,你先本地先这样改下,下个版本发布这个特性。

  • liziyu 2022-05-03

    期待!

年代过于久远,无法发表回答
🔝