欢迎各位兄弟 发布技术文章
这里的技术是共享的
来自 https://laracasts.com/discuss/channels/laravel/add-query-string-to-current-url
例如: Route::controller('spiel','SpielController');
$u = URL::action('SpielController@getIndex', array(
'term' => 'termb',
'link' => 'type'
));
var_dump($u);
> string 'http://zrw2014vorsorge.dev/spiel/index/termb/type' (length=49)
我需要: 字符串' http://zrw2014vorsorge.dev/spiel/index?term=termb&link=type '
这是在 Laravel 4.0 上工作并在 Laravel 4.1 上停止 - 有没有其他方法可以做到这一点?
您可以使用命名路由:
Route::get('spiel', array('as' => 'spiel.index', 'uses' => 'SpielController@getIndex'));
//then
$u = URL::route('spiel.index', array(
'term' => 'termb',
'link' => 'type'
));
或者只是附加一个查询字符串(虽然看起来有点难看):
$params = array('term' => 'termb', 'link' => 'type');
$queryString = http_build_query($params);
$u = URL::to(action('SpielController@getIndex') . '?' . $queryString);
在 4.2 中,我可以轻松地使用它:-
$params = array('term' => 'termb', 'link' => 'type');
$queryString = http_build_query($params);
URL::action('ProfileController@show', '?'.$queryString )
不确定,大约 4.1
更新后我也遇到这个问题
//Retrieve current query strings: $currentQueries = $request->query(); //Declare new queries you want to append to string: $newQueries = ['foo' => 'bar', 'popular']; //Merge together current and new query strings: $allQueries = array_merge($currentQueries, $newQueries); //Generate the URL with all the queries: $request->fullUrlWithQuery($allQueries); # 这是个好方法