Url with queryParams

I’ve a controller action orders/export-csv wich exports a csv file.
I call it with a link from a view (with PJax) where is the gridView widjet.
I want to export the filtered models shown in the grid.

The link should have the correct parameters to make a search in ordersSearch model

$searchModel = new OrdersSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

I tryied with
but it doesn’t work, the csv is not filtered, all models are exported.

In the browser developer tools I see that the parameters passed by my link are

1[OrdersSearch][order_number]: 89

while they shoud be (I think)

OrdersSearch[order_number]: 89

Any help?
Thank you!

You could check on db log to verify the query is correct or not.

The query is not correct because the parameters are not loaded in the search model.

from documentation,

use yii\helpers\Url;

// Url::to() calls UrlManager::createUrl() to create a URL
$url = Url::to(['post/view', 'id' => 100]);

your params is not usually. whether it is support or not if you using Yii::$app->request->queryParams

Thank you Wilson!
I tried also
$url = Url::to(['orders/export-csv', 'order_number' => 81]);
But it doesn’t work.
I need to get all params in the filters and send them to the controller.

In Yii1 I used:

Yii::app()->clientScript->registerScript('export', "
$('#export-button').on('click',function(e) {

    $.fn.yiiGridView.export = function() {
            success: function() {
                window.location = '" . $this->createUrl('exportFile') . "';
            data: $('.search-form form').serialize() + '&export=true'


Ok, almost done.
The correct link is:
Url::to(['orders/export-csv', Html::getInputName($searchModel,'order_number')=>Html::getAttributeValue($searchModel,'order_number')]);
Now i need to get all filters and build url dinamically

You may try this instead.

$array = Yii::$app->request->queryParams;
array_unshift($array, 'orders/export-csv');

Thank you softark :+1:

1 Like