如何为我的网页制作像 Facebook 一样的用户个人资料链接 [英] How to make user profile link like facebook for my web page

查看:26
本文介绍了如何为我的网页制作像 Facebook 一样的用户个人资料链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Yii 框架开发一个网页,我想做像 facebook 一样的 url 管理.这是我的网址 www.mywebpage.com/user/profile/id/10 但我不想这样显示这个网址,我想这样显示 www.mywebpage.com/yogi 我可以在 Yii 上做吗?请任何人帮助我.

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.

推荐答案

查看本文以开始使用:http://yiitutorials.net/easy/easy-url-rewriting-with-yii

您基本上将在数据库中存储用户自定义 URL.因此,示例 URL 将是 http://example.com/user/userurl

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

该规则定义了一个带有http://example.com/user/"的 URL,斜杠后面的部分可以被一个名为customurl"的 GET 变量访问.然后,您可以像这样访问用户自定义 URL:

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']."'");

如下所述,这里是一个来自真实网站.在此示例中,URL 如下所示:http://website.com/blog/{post_title}/{post_id}

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' 

所以在我们的博客控制器中,我们有一个名为 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']."'");
    ...
}

因此,任何具有 london-blog/some_value/some_value 格式的 URL 都将指向配置中指定的控制器操作.然后,您可以使用配置中定义的变量名称访问 some_value 部分(< :.+> 中的位)

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 < :.+>)

希望有帮助!

这篇关于如何为我的网页制作像 Facebook 一样的用户个人资料链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆