Search code examples
phpyii2url-routingyii2-basic-app

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


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

Solution

  • Change this line

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

    to this

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

    That should do it