【问题标题】:How to create custom url in Yii [duplicate]如何在 Yii 中创建自定义 url [重复]
【发布时间】:2012-10-29 11:22:53
【问题描述】:

可能重复:
How to make user profile link like facebook for my web page

我如何管理用户个人资料的自定义网址,例如

www.exampl.com/brito  insted of  www.exampl.com/user/profile/id/22
www.exampl.com/jhon   insted of  www.exampl.com/user/profile/id/31
www.exampl.com/piter  insted of  www.exampl.com/user/profile/id/66

我在我的 config/main.php 文件中完成了以下代码,现在它仅适用于这 3 个静态用户。如何为所有 1000 个用户动态更改它?

  urlManager'=>array(
                'urlFormat'=>'path',
                'showScriptName'=>false,
                'rules'=>array(
                    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                    'brito'=>'user/profile/id/22',
                    'john'=>'user/profile/id/31',
                    'piter'=>'user/profile/id/66',
                ),
            ),

请任何人帮助我。

【问题讨论】:

  • 首先,查看 yii 社区提供的内容;我相信你会在那里找到一个 url 友好的扩展/插件/模块

标签: php yii url-mapping


【解决方案1】:

使用 htaccess

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# do not do anything
RewriteRule ^ - [L]

# forward /blog/foo to blog.php/foo
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]

# forward /john to user/profile/?name=john
RewriteRule ^((?!blog/).+)$ user/profile/?name=$1 [L,QSA,NC]

并确保您必须在页面中包含name

【讨论】:

  • 我不想在查询字符串中发送名称,我想在查询字符串中发送用户 ID 我该怎么做? john id 是 31 那么我如何在 htaccess 或 config/main.php 中动态获得 31?
  • 我认为这是不可能的,你不能在 htaccess 文件中执行数据库操作,我认为你也不能在 main.php 配置中编写这样的规则..
  • 是的,非常感谢!我是通过其他方式完成的,但您的回答对我很有用。
  • 很高兴知道,你可以分享你使用的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 2015-06-28
  • 1970-01-01
  • 2017-03-11
  • 2012-12-21
  • 1970-01-01
相关资源
最近更新 更多