Ionut Flavius Pogacian
Ionut Flavius Pogacian

Reputation: 4811

Yii: How to redirect and go to a specific element on page?

I am trying to redirect and go to a specific element on the new page like this:

http://192.168.0.49/x/y/index.php/admin/user/update/id/3#certificate

$this->redirect(array('update', 'id' => $certificate->user_id));

How can this be done?

Upvotes: 0

Views: 204

Answers (2)

Jon
Jon

Reputation: 437854

You can simply create the url without the fragment part and then append it manually:

$url = Yii::app()->createUrl('update', ['id' => $certificate->user_id]);
$url .= "#certificate";

$this->redirect($url);

This code works in a manner that is immediately obvious when reading the code. Apart from that there is also the Yii-specific solution: CUrlManager (the component responsible for building URLs) also recognizes # as a parameter. So you can write:

$url = Yii::app()->createUrl(
    'update',
    ['id' => $certificate->user_id, '#' => 'certificate']
);

Upvotes: 2

Aidan
Aidan

Reputation: 109

That can't be done using redirect. A work around would be

$url = Yii::app()->createUrl('update', array('id' => $certificate->user_id, '#' => "certificate"));
$this->redirect($url);

Upvotes: 1

Related Questions