How to create custom url in Yii [duplicate]

2019-04-01 18:54发布

Possible Duplicate:
How to make user profile link like facebook for my web page

How do i manage custom url for user profile like

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

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

Please help me any one.

1条回答
Root(大扎)
2楼-- · 2019-04-01 19:11

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]

and make sure u must include the name in the page

查看更多
登录 后发表回答