how to redirect and add # in the end


(Boynet2) #1

i want to redirect my page to this url:

http://example.com/index.php?r=projects/update&id=28#unieq28

i tried a lot of ways with $this->redirect they all replace the # with %23

tnx


(Ricard Buveur) #2

As far as I know, you can’t server-side redirect to the hash part of a link - the hash part is only managed by the client (browser), so while you can generate a link including a hash, that will work if a user clicks on it in his browser (because the browser will load the relevant page then look for the anchor in it), server side you can only redirect to a page and not within that page. This is inherent in HTTP/HTML and nothing to do with Yii.


(Boynet2) #3

sorry but i think you did not understand me

i have a website in one of my page i have a div with id="example"

now if i do something like <a href="www.mywebsite.com/projects/index#example" /> it works great

i want to do the same in yii so i tried:


 $this->redirect(array('projects/view#example'));

and it not working

so how do i make a link in yii and add in the end the # symbol?


(Ricard Buveur) #4

$this->redirect (referring to the Controller) is used to automatically redirect an incoming request to a different url, which is a server-side action (not generating a clickable link on a web page), so cannot involve the hash part of the url.

If you mean that you want to generate a link in your view that the user can then click on, which should include a hash, then that is a different question.

I haven’t seen any parameters in createUrl for including a hash part, but at the very worst, in your view you would do something like


echo $this->createUrl('projects/index').'#example';


(Boynet2) #5

yes i want to redirect.

and tnx it works;


                    

$url = Yii::app()->createUrl('projects/view')."&id=$model->projectID#uniqe_$id";      


$this->redirect($url);



its kind of a hack but it works


(Ricard Buveur) #6

It doesn’t even need to be that hacky…


$url = Yii::app()->createUrl('projects/view', 'id' => $model->projectID)."#uniqe_$id";

should give you the same result, as well as allowing you to change your url scheme later without recoding if necessary (apart from the hash fragment of course).

And yes, I suppose if you are redirecting the url via a 302 code (not an invisible redirect), that should work - I hadn’t though about it like that.


(Boynet2) #7

thank you

and you forget the array(for anyone in the future that gonna search for that


$url = Yii::app()->createUrl('projects/view',array( 'id' => $model->projectID))."#uniqe_$id";


(Adinugro) #8

I am not sure if this is you’re looking for…

see this thread

In short,




Yii::app()->createUrl('projects/view', 'id' => $model->projectID, '#' => "#uniqe_$id");




(Ricard Buveur) #9

Good to know - I hadn’t come across that before.


(jeyakumar) #10

There is a small mistake in it.(extra # in value of # key)

And the correct one is
Yii::app()->createUrl(‘projects/view’, ‘id’ => $model->projectID, ‘#’ => “uniqe_$id”);