2015-10-07 25 views
3

一旦我在URL中傳遞41, confirm.php打印41.不接受字母數字參數的網址 - Yii2-app-basic

http://localhost/yii2-app-basic/web/site/confirm/41

但是,當我通過 「cfeb70c4c627167ee56d6e09b591a3ee」 或URL 「41A」,

http://localhost/yii2-app-basic/web/site/confirm/41a

它顯示錯誤

NOT FOUND (#404)
找不到頁面。

上述錯誤發生在Web服務器處理您的請求時。如果您認爲 這是服務器錯誤,請與我們聯繫。謝謝。

我想向用戶發送確認ID以確認他們的帳戶。 這就是爲什麼隨機數「cfeb70c4c627167ee56d6e09b591a3ee」正在通過。

那麼,我能做些什麼來使URL接受字母數字參數。

配置/ web.php

'urlManager' => [ 
     'showScriptName' => false, 
     'enablePrettyUrl' => true, 
     'enableStrictParsing' => false, 
     'rules' => [ 
      '<controller>/<action>/<id:\d+>' => '<controller>/<action>' 
     ], 
    ], 

SiteController.php

public function actionConfirm($id) 
{ 
    $id = Yii::$app->request->get('id'); 
    $this->view->params['customParam'] = $id; 

    return $this->render("confirm",array("id"=>$id)); 
} 

回答

4

改變這一行

'<controller>/<action>/<id:\d+>' => '<controller>/<action>' 

'<controller>/<action>/<id:[a-z0-9]+>' => '<controller>/<action>' 

應該這樣做

+0

非常好的@Paladin先生。還有一件事,你是否有任何文檔描述了這種類型的URL參數。如果是的話,請與我分享。 –

+0

我會在幾分鐘內接受答案。它工作正常。謝謝 –

+1

在這裏,您可以鏈接到2.0文檔:http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html – Paladin

2

目前的規則規定的id是一個數字(\d+),因此它不會在你的例子工作。而不是修改現行規則,我會爲此案例添加一個:

'rules' => [ 
    'site/confirm/<id:\w+>' => 'site/confirm', 
    '<controller>/<action>/<id:\d+>' => '<controller>/<action>' 
], 
+0

是的。這一個也工作。尼斯。謝謝。 –

相關問題