Url with queryParams

Hi!
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
Url::to(['orders/export-csv',Yii::$app->request->queryParams])
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
1[OrdersSearch][reference]:
1[OrdersSearch][created_at]:

while they shoud be (I think)

OrdersSearch[order_number]: 89
OrdersSearch[reference]:
OrdersSearch[created_at]:

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();
    e.preventDefault();
});

    $.fn.yiiGridView.export = function() {
        $.fn.yiiGridView.update('fattureRighe-grid',{ 
            success: function() {
                $('#fattureRighe-grid').removeClass('grid-view-loading');
                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');
Url::to($array);
3 Likes

Great!
Thank you softark :+1:

1 Like