I have Users pages. In index view they all have hyperlinks on ID, and so they are sending ID to model. And when u press the link the users View is loaded and the url becomes
/site/users/1
But what I want is the URL to be not users ID but users Username like
/site/users/Alex
How can I do it ? This is my _view
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
Controller
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
You will need to modify the following files :-
main.php(config)
"urlManager" => array(
"rules" => array(
"/site/users/<name:[\w\_]>" => "/site/users/view",
//All the other rules here...
)
)
Controller
public function actionView($name){
$mdoel = User::model()->findByAttributes(array(
"username" => $name
));
if($model){
$this->render("view", array(
"model" => $model
));
}
}
And wherever you are putting a link, make the link as follows :-
<?php echo CHtml::link(CHtml::encode($data->id), array("view", "name" => $data->username)); ?>
Update :-
Try using the following syntax :-
<?php echo Chtml::link(CHtml::encode($data->id), $this->createUrl('view', array('name'=>$model->username))); ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.