mithlesh
(Mithlesh1007)
October 24, 2012, 7:54am
1
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
redguy
(Maciej Lizewski)
October 24, 2012, 8:22am
2
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 );
}
mithlesh
(Mithlesh1007)
October 25, 2012, 2:07pm
3
redguy:
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.
redguy
(Maciej Lizewski)
October 25, 2012, 2:30pm
4
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.
wendwessen
(Wendwessen)
October 25, 2012, 3:47pm
5
you don’t need to pass ‘id’ in the query string you can get the logged-in user id using Yii::app()->user-id
mithlesh
(Mithlesh1007)
October 27, 2012, 9:00am
6
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
Bianca
(Biancajsen)
October 27, 2012, 10:34am
7
Mithlesh:
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
redguy
(Maciej Lizewski)
October 27, 2012, 10:43am
8
Mithlesh:
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
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.
mithlesh
(Mithlesh1007)
October 28, 2012, 5:29am
9
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
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.