Hide User Id In Url

Hi,

I have created a user profile, when the user see his profile this thing is shown in url "profile/view/id/37"

I dont want the user should see his id it should be only shown as "profile/"

I have used url manager for this but still i can see the user id in the url "profile/37"




'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(							

			'<controller:\w+>/<id:\d+>'=>'<controller>/view',

			'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

			'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),



Can anyone please suggest how to not show id in the url.

Thanks in advance

just do not pass id to createUrl method then it will not show up in url string.

in action you would probably implement some "default value":




if( isset( $_GET['id'] ) ) {

   $user = User::model()->findByPk( $_GET['id'] );

} else {

   $user = User::model()->findByPk( Yii::app()->user->id );

}



Hi redguy,

Can you please brief me in this or provide me an example.

I am not unable to get you.

appreciate if you could provide me an example on this.

Thanks in advance.

if you create links with:


CHtml::link( 'profile', array( '/user/profile', 'id'=111 ) );

then you will have ‘id’ in generated URL. if you do not add this param - it won’t show in URL:


CHtml::link( 'profile', array( '/user/profile' ) );

if you want to further shorten the generated URL, add urlManager rule:




'profile'=>'user/profile',

'profile/<id:\d+>'=>'user/profile',



(they will work ok in both cases: with and without passed ‘id’)

and last but not least you have to prepare your ‘profile’ action to work correctly with and without ‘id’ param and that part I did already describe in my first post.

you don’t need to pass ‘id’ in the query string you can get the logged-in user id using Yii::app()->user-id

Hi redguy,

I am sorry to say but that dosen’t work, may be i am doing something wrong…

Find the code below i used in my site controller. when the user logins he will be redirected to his profile page.

when he sees his profile in the url this is shown "profile/view/id/37"

I dont want the user should see his id in the url it should be only shown as "profile/"




if($model->validate() && $model->login())  

			{  

				 $user = User::model()->findByPk(Yii::app()->user->Id);  

			  

				 if($user->status == 'active')                           

					 $this->redirect(array("/news/index"));  

			 

				 else  $this->redirect(array("/user/view/id/".Yii::app()->user->Id));

											 

											 

			}



Same thing happens here, when the user clicks on the profile link, his profile is shown but in the url this is displayed "profile/view/id/37" which i dont want.




array('label'=>'Profile','url'=>array('user/view/id/'.Yii::app()->user->Id),'visible'=>!Yii::app()->user->isGuest),



Thanks in advance

Hi Mithlesh,

You should not pass the id as parameter at all for it not to be displayed in the url.

Your code should be like this:




$this->redirect(array("/user/view/id/"));



But then you should modify your view action in your usercontroller so that it works even when the user id is not passed as parameter, i.e. in the case this action is fired after a login. In this case, the action will run on the id of the user who has logged in.

For example, your view action in your user controller could be like this:-





	public function actionView($id=null)


	{

		if($id != null)		

		$this->render('view',array(


			'model'=>$this->loadModel($id),


		));

		else {	$id = Yii::app()->user->Id;

			$this->render('view',array(


			'model'=>$this->loadModel($id),


		));	

		}


	}



UPDATE

Oops, sorry, I forgot to remove id from the code. It should be like this:-




$this->redirect(array("/user/view"));// without id parameter



do not pass ‘id’ param in if you do not want it in generated URL:




$this->redirect(array("/user/view"));






array('label'=>'Profile','url'=>array('user/view'),'visible'=>!Yii::app()->user->isGuest),



simple as that.

Hi Jimlam,

Thanks for the code it worked very well as expected…

Hi Redguy,

Thank you too…

Inorder the code to work we should have to modify our actionview in controller which Jimlam has provided,

Otherwise it will give "Error 400 Your request is invalid".

Thank you both once again for your valuable time and effort.