更改网址格式以接受用户名(如Facebook) [英] Changing pattern of url to accept username like facebook

查看:243
本文介绍了更改网址格式以接受用户名(如Facebook)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从此隐藏用户个人资料链接 http://example.com/site/index?user_id=sami.yaqoub

I need to covert user profile link from this http://example.com/site/index?user_id=sami.yaqoub

要像Facebook http://example.com/sami.yaqoub

To be like Facebook http://example.com/sami.yaqoub

我将配置文件的规则改为了.

I changed the rules of config file to except that.

Config.php

Config.php

<?php
..
 '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>',
                '<user:[a-zA-Z0-9_-]\w+>'=>'site/index',// here is the code 
            ),
        ),
...
?>

它适用于所有不包含点."的单词. ,所以我将其更改为这样

It worked with all words not contained any dot "." , so I changed it to be like this

 '<user:[a-zA-Z0-9_-.]\w+>'=>'site/index',// return error

但是也没有用. 除了这种公式,在这种情况下最好的方法是什么 名称.任何东西

But also not worked . What is the best method in this case to except this formula Name.anything

预先感谢

推荐答案

在您的正则表达式[a-zA-Z0-9_-.]\w+中,您可以匹配.helloworld之类的东西,但不能匹配hello.world,因为仅在第一个位置匹配点.字符.

In you regexp [a-zA-Z0-9_-.]\w+ you can match things like .helloworld but not hello.world because you match the dot . character only in first position.

您应该这样写:[a-zA-Z0-9_-.][\w.]+.

我不确定,但也许Facebook不允许在第一位置使用特殊字符,例如点或破折号.-.在这种情况下,正确答案应为:[a-zA-Z0-9][\w.]+或更短的\w[\w.]+

I am not sure, but maybe Facebook does not allow special characters in first position such as dot or dash.-. In that case the correct answer would be : [a-zA-Z0-9][\w.]+ or shorter \w[\w.]+

请注意,正则表达式中的\w与单词字符匹配. \w等同于[A-Za-z0-9_]

Note that \w in a regexp matches word characters. \w is equivalent to [A-Za-z0-9_]

这篇关于更改网址格式以接受用户名(如Facebook)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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