I am developing a web page using Yii framework, I want to do url management like facebook. This is my url www.mywebpage.com/user/profile/id/10 but i dont want to show this url like this, i would like to show like this www.mywebpage.com/yogi can i do it in Yii? please anyone help me.
问题:
回答1:
Check this article out to get started: http://yiitutorials.net/easy/easy-url-rewriting-with-yii
You will bascially be storing the users custom URL in the database. So an example URL would be http://example.com/user/userurl
In your main config you could set up a rule like:
user/<customurl:.+> => 'user/view' //second part is the controller/action
That rule defines that a URL with 'http://example.com/user/', the part after the slash can be accessed by a GET variable with the name 'customurl'. You can then access the users custom URL like so:
$_GET['customurl'];
And query the user record something like so:
$user = User::model()->find("customurl = '".$_GET['customurl']."'");
As mentioned below, here is an example from a real website. In this example the URL looks like so: http://website.com/blog/{post_title}/{post_id}
So the rule for this would be something like:
london-blog/<post_title:.+>/<post_url:.+> => 'blog/viewpost'
So in our blog controller, we have an action called viewpost (see above how the rule is pointing to this controller/action?), which would look something like:
public function actionViewpost(){
$blogpost = Blog::model()->find("post_title = '".$_GET['post_title']."'");
...
}
So any URL that has the format london-blog/some_value/some_value will point to the controller action as specified in the config. You can then access the some_value parts using the variable names defined in the config (the bits in the < :.+>)
Hope that helps!
回答2:
I think all the information that you need is here: http://www.yiiframework.com/doc/guide/1.1/en/topics.url