正在學(xué)習(xí)webman,我想生成一個(gè)帶不確定數(shù)量get參數(shù)的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參數(shù)
我希望生成/edit.html?id=8&uid=1&from=admin.....(很多不確定的get參數(shù)),應(yīng)該如何處理?
萌新一枚,希望各位大佬解答一下
我嘗試修改了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;
}
現(xiàn)在可以將匹配后剩余的參數(shù)以?的形式綴在route()函數(shù)生成的URL后面,但是我不確定這種方法有沒有其他后遺癥