从网址加密/隐藏控制器和操作名称

时间:2016-02-12 06:12:22

标签: php url encryption yii

在我的yii web应用程序中,出于安全目的,我想隐藏或加密url中的控制器和操作名称。 在我的config / main.php中,

 'urlManager' => array(
        'urlFormat' => 'path',
        'rules' => array(
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
    ),

现在url是,

http://localhost/webschool/index.php/core/student/create

我想要更改此网址,

http://localhost/webschool/

http://localhost/webschool/uUG32376HJBDwg2366Gh_308

请帮帮我

提前致谢.....

1 个答案:

答案 0 :(得分:1)

您可以定义自己的custom-url-rule-class,例如:

class CustomUrlRule extends CBaseUrlRule
{
    public function createUrl($manager,$route,$params,$ampersand)
    {
        if ($route==='core/student/create')
        {
            // here use your own encryption logic
            return base64_encode($route);
        }
        return false;  // this rule does not apply
    }

    public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
    {
        // here use your own decryption logic
        $decoded = base64_decode($pathInfo);
        if ($decoded==='core/student/create') {
            return $decoded;
        }
        return false;  // this rule does not apply
    }
}

然后在配置的UrlManager部分声明它:

'urlManager' => array(
    'urlFormat' => 'path',
    'rules' => array(
        // my custom rule (first one)
        array(
            'class' => 'application.components.CustomUrlRule',
            'connectionID' => 'db',  // if necessary for your logic
        ),
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
),