URL not accepting alpha numeric parameter - Yii2-app-basic

瘦欲@ 提交于 2019-12-02 01:29:10

问题


As soon, i'm passing 41 in URL. confirm.php prints 41.

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

But, when i pass "cfeb70c4c627167ee56d6e09b591a3ee" or "41a" in URL,

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

it shows error

NOT FOUND (#404)
Page not found.

The above error occurred while the Web server was processing your request. Please contact us if you think this is a server error. Thank you.

I want to send confirmation id to user to confirm their account. That's why random number "cfeb70c4c627167ee56d6e09b591a3ee" is being passed.

So, what can i do to make URL accept alpha numeric parameter.

config/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));
}

回答1:


Change this line

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

to this

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

That should do it




回答2:


The current rule states that id is a number(\d+) hence it not working in your examples. Instead of modifying the current rule, I would add one specifically for this case:

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


来源:https://stackoverflow.com/questions/32988996/url-not-accepting-alpha-numeric-parameter-yii2-app-basic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!