“美化"Yii 中的 URL [英] "Beautifying" a URL in Yii
问题描述
我想转换一个格式的网址
I want to convert a URL which is of the format
path/to/my/app/Controller_action/id/2
path/to/my/app/Controller_action/id/2
到
path/to/my/app/Controller_action/id/User_corresponding_to_id_2
path/to/my/app/Controller_action/id/User_corresponding_to_id_2
我已经看过这个来自 Yii 的教程,但它对我没有任何帮助.有人能帮我解决这个问题吗?
I have already seen this tutorial from Yii, but it isnt helping me with anything. Can anyone help me with this?
编辑:我还想知道这件事在 POST 场景中是否可行,即我在 URL 中只会有 path/to/my/app/Controller_action.
EDIT: I would also like to know if this thing is even possible in the POST scenario, ie I will only have path/to/my/app/Controller_action in the URL.
推荐答案
在您的 User 模型中添加 getUrl 方法
Add a getUrl method in your User model
public function getUrl()
{
return Yii::app()->createUrl('controller/action', array(
'id'=>$this->id,
'username'=>$this->username,
));
}
在config/main.php
'controller/action/<username:.*?>/<id: \d+>'=>'controller/action'
并在任何地方使用模型 url
虚拟属性
And use the models url
virtual attribute everywhere
这篇关于“美化"Yii 中的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!