在 PHP 中为个人用户实现个性化 URL
如何在 PHP 中为每个用户实现一个个性化 URL?我正在将我的网络应用程序的登录系统实现为 Drax LLP 登录系统的调整版本。
因此,每个用户都应该能够修改他的个人资料,该个人资料最终将出现在他的虚荣 URL 上……例如 xyz.com/user。
有什么建议/想法吗?谢谢..
How would one go about implementing a vanity URL for each user in PHP? I'm implementing my web-app's login system as a tweaked version of the Drax LLP Login system.
So, each user should be able to modify his profile which will finally appear on his vanity URL .. like xyz.com/user.
Any tips / ideas? Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是所涉及文件的示例:
.htaccess:
index.php
上面的函数应该成为前端控制器类中某处的方法。您只需从 $location 中减去基本路径,这样就剩下虚拟路径了。然后您可以做任何您想做的事情——例如,简单地按“/”进行拆分,然后按照您的方式处理路径,根据 URI 将每个剩余部分传递到下一个控制器。您还可以使用正则表达式编写更复杂的路由规则(Django 和许多其他框架就是这么做的)。
例如:
/profiles/hardik988
将被传递到负责“配置文件”的控制器,然后该控制器决定如何处理路径的其余部分。它可能会查找用户名并显示适当的模板。
Here's an example of the files involved:
.htaccess:
index.php
The function above should become a method in your front controller class somewhere. You just need to subtract your base path from $location so you're left with the virtual path. Then you can do whatever you want -- e.g. simply split on '/' and then work your way through the path, passing each remainder to the next controller depending on the URI. You could also write more complicated routing rules using regular expressions (something Django and quite a few other frameworks do).
For example:
/profiles/hardik988
Would be passed to the controller in charge of 'profiles', which then decides what to do with the remainder of the path. It would likely look up the username and display the appropriate template.
它相对简单:您有一个
mod_rewrite
规则,可以将请求www.domain.com/username
映射到www.domain.com/users/username< /代码>。
但是,您需要了解禁止哪些内容作为用户名,因为我假设您还有其他顶级页面,例如
www.domain.com/about
或www.域名.com/terms
。因此,您不希望人们将about
和terms
注册为用户名。It's relatively simple: you have a
mod_rewrite
rule to map requests towww.domain.com/username
to something likewww.domain.com/users/username
.However, you then need to be aware what to prohibit from being a username, as I presume you'll have other top-level pages such as
www.domain.com/about
orwww.domain.com/terms
. Therefore, you don't want people registeringabout
andterms
as usernames.