sam
sam

Reputation: 956

Creating URL and params in Yii2

I'm trying to create Url with multiple param using Url::to() method but it's not working i also tried UrlManager->creatUrl() method but no luck, i have pretty Url enable and if i use only one query param it works fine but when i try to add more i got error, below is my code.

$linkUrl = \Yii::$app->UrlManager->createUrl(['sell/updatecategory', ['draftId'=> $model->draftId,'catid' =>$model->category_id]]);
<?= Html::button('Change category or product',['value'=>$linkUrl, 'id' => 'StartOver']) ?>
another try is:
$linkUrl = Url::to(['sell/updatecategory', ['draftId'=> $model->draftId,'catid' =>$model->category_id]]);

in the two case above the url output always look like this:

GET http://http://192.168.199.128/trobay/products/sell/updatecategory?1%5BdraftId%5D=20&1%5Bcatid%5D=50

and the server throw an error cannot resolve the url, what i want is something like this:

GET http://http://192.168.199.128/trobay/products/sell/updatecategory?draftId=20&catid=50

The system added some character which i guess is the cause of the problem but don't really know how to remove this. i hot anyone could help with this thanks

Upvotes: 0

Views: 2639

Answers (1)

Bizley
Bizley

Reputation: 18021

Don't use nested array for parameters. Structure should look like this:

[0 => route, param1 => value1, param2 => value2, ...]

so in your case

$linkUrl = Url::to([
    'sell/updatecategory', 
    'draftId' => $model->draftId, 
    'catid' => $model->category_id
]);

Upvotes: 1

Related Questions