Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

php - Laravel route url with query string

On laravel 4 I could generate a url with query strings using the route() helper. But on 4.1 instead of:

$url = url('admin.events', array('lang' => 'en'));
// admineventsurl/?lang=en

I get:

$url = url('admin.events', array('lang' => 'en'));
// admineventsurl/en

I did some research and all laravel methods to generate url are using the parameters like that. How can I generate the url with query strings?

question from:https://stackoverflow.com/questions/21632835/laravel-route-url-with-query-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Laravel's route() and action() helper methods support URL query params. The url() helper method, unfortunately does not.

Simply provide an array with key values to the route parameters. For example:

route('products.index', ['manufacturer' => 'Samsung']);

// Returns 'http://localhost/products?manufacturer=Samsung'

You can also still include your route parameters (such as ID's and models) to accompany these parameters:

route('products.show', [$product->id, 'model' => 'T9X']);

// Returns 'http://localhost/products/1?model=T9X'

Basically, any elements in the array that contain string keys will be treated as query parameter (/products?param=value). Anything with an integer key will be treated as a URL argument (/products/{arg}).

This is also supported in action methods:

action('ProductController@index', ['manufacturer' => 'Samsung']);

You can also supply query parameters inside the link_to_route() and link_to_action() methods:

link_to_route('products.index', 'Products by Samsung', ['model' => 'Samsung');

link_to_action('ProductController@index', 'Products by Samsung', ['model' => 'Samsung']);

2019 - EDIT:

If you can't use route() or action(), you can generate a URL with query params using the Arr::query() helper:

url('/products?').IlluminateSupportArr::query(['manufacturer' => 'Samsung']);

// Returns 'http://localhost/products?manufacturer=Samsung'

Or:

url('/products?').http_build_query(['manufacturer' => 'Samsung'], null, '&', PHP_QUERY_RFC3986);

// Returns 'http://localhost/products?manufacturer=Samsung'

Or create a simple helper function:

use IlluminateSupportArr;
use IlluminateSupportStr;

function url_query($to, array $params = [], array $additional = []) {
    return Str::finish(url($to, $additional), '?') . Arr::query($params);
}

Then call it:

url_query('products', ['manufacturer' => 'Samsung']);

// Returns 'http://localhost/products?manufacturer=Samsung'

url_query('products', ['manufacturer' => 'Samsung'], [$product->id]);

// Returns 'http://localhost/products/1?manufacturer=Samsung'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...