L
L
LittleFatNinja2016-09-15 11:43:37
Yii
LittleFatNinja, 2016-09-15 11:43:37

How to create a link with get parameters in yii2?

using the string 'http:/bla.com' + params ['foo' => 5, 'bar' => 2]
get 'http:/bla.com?foo=5&bar=2'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mitaichik, 2016-09-15
@LittleFatNinja

In yii there is such concept "route" - a route. ( https://github.com/yiisoft/yii2/blob/master/docs/g... ) It contains a link to the controller\action + parameters. Written as an array ['/controller/action', 'param1' => 'val1', 'param2' => 'val2'].
Most methods (the same Html::a) can accept this route as a URL, and Yii will already generate normal URLs in accordance with the rules. By default, it shoves the parameters into the query part of the url, that is, you get a url like
http:/bla.com/controller/action?param1=val1¶m2=val2
But you can configure it for anyone, for example, http:/bla.com/controller/action/ val1/val2 In this case, the code with routes does not have to be rewritten.
In general, everything in yii is well thought out and cool. Personally, we always specify urls as routes - this is really very convenient.

D
Dmitry, 2016-09-15
@slo_nik

Good afternoon.
For example

Html::a('Link',
            ['controller/action'],
            [
                 'data' => [
                      'method' => 'post',
                      'params' => '{"id":'.$model->id.'}'
                 ],
            ]);

The link is passed by the post method and the required id is passed
or so
Html::a('Link',
            ['controller/action' . $model->id],
           );

or this option
Html::a('Link',
            Url::toRoute(['product/view', 'id' => 42, 'name' => 'name', 'id_s' => 2]));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question