Add Extra Arguments In Current Url

Hi all,

I have a view and I want to make a link that has the current url (with current $_GET parameters) plus one more

Two candinates way to do that are


$this->createUrl('', array('new_param'=>'value-param')) //but generates

index.php?r=controller/action&new_param=value-param


Yii::app()->createUrl(CHtml::normalizeUrl(''), array('new_param'=>'value-param')) //but generates

index.php?r=nameapplication/index.php?r=controller/action&cur_par1=value1&cur_par2=value2&new_param=value-param

But I want the obvious


index.php?r=controller/action&cur_par1=value1&cur_par2=value2&new_param=value-param

I thing I am very closed on this but I cant find out!

Note the old parameters are unkown until the application runs and I don’t want something hacking like that


Yii::app()->request->requestUri . '&new_param=value-param'

Any suggestion?

How about:




   $params = array_merge($_GET, array('yourKey1'=>'yourValue1', ...));



It’s pretty ugly, but it does prevent you from duplicating existing keys - they’ll be overridden by your second array.

Hi keith!

Although I would like something more yii’s way it will be work.

In this way the extra parameters not repeated again and again among the requests (if the link exists on the other requests)

I thing a problem with this way is the SEO url compatibility with UrlManager… is’t it ?

I think it will work fine for you. What problem do you envisage exactly?

I thought it better and I thing that there is no problem :)

I hoped there was Yii way to do that and I stuck :blink:

Thanks Keith

This reply is for new people:

$params = Yii::$app->request->getQueryParams() != null ? Yii::$app->request->getQueryParams() : [];
$params[0] = Yii::$app->controller->getRoute();
$params['new_param'] = 'value-param';

After that you can use: Yii::$app->urlManager->createUrl($params) where you need to create a url.

(Yii2)

Yii includes many helpers, like de Url helper, which includes the method Current that accepts new params.

\yii\helpers\Url::current(['yournewparam' => 'valuefortheparam'])

(Yii2)

2 Likes

this is perfect! thank you so much