如何在 Yii 中创建自定义 url [英] How to create custom url in Yii

查看:24
本文介绍了如何在 Yii 中创建自定义 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何制作像 facebook 一样的用户个人资料链接对于我的网页

我如何管理像

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 个用户动态更改它?

I have done below code in my config/main.php file, now its works for this 3 static users only. how can i change it dynamically for all 1000 of users?

  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',
                ),
            ),

请帮助我.

推荐答案

use htaccess

use 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

and make sure u must include the name in the page

这篇关于如何在 Yii 中创建自定义 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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