2016-02-12 154 views
1

在我的yii Web應用程序中,爲了安全起見,我想隱藏或加密URL中的控制器和操作名稱。 在我的config/main.php,從url中加密/隱藏控制器和操作名稱

'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 

請幫我

在此先感謝.....

+0

竇在問之前讀了這個? http://www.yiiframework.com/doc/guide/1.1/en/topics.url#faking-url-suffix – SiZE

+0

[你不想加密URL參數](https://paragonie.com/blog/ 2015/09 /全面導-URL參數的加密功能於PHP)。 –

+0

我試過了,但無法解決我的問題。我不知道如何更改我的urlmanager。請幫幫我。 – Arya

回答

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>', 
    ), 
), 
相關問題