URL 不接受字母数字参数 - Yii2-app-basic
URL not accepting alpha numeric parameter - Yii2-app-basic
很快,我将在 URL 中超过 41。 confirm.php 打印 41。
http://localhost/yii2-app-basic/web/site/confirm/41
但是,当我在 URL 中传递 "cfeb70c4c627167ee56d6e09b591a3ee" 或“41a”时,
http://localhost/yii2-app-basic/web/site/confirm/41a
显示错误
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.
我想向用户发送确认 ID 以确认他们的帐户。
这就是传递随机数 "cfeb70c4c627167ee56d6e09b591a3ee" 的原因。
那么,我该怎么做才能让 URL 接受字母数字参数。
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));
}
更改此行
'<controller>/<action>/<id:\d+>' => '<controller>/<action>'
至此
'<controller>/<action>/<id:[a-z0-9]+>' => '<controller>/<action>'
应该就可以了
当前规则规定 id 是一个数字 (\d+
),因此它在您的示例中不起作用。我不会修改当前规则,而是专门为这种情况添加一个规则:
'rules' => [
'site/confirm/<id:\w+>' => 'site/confirm',
'<controller>/<action>/<id:\d+>' => '<controller>/<action>'
],
很快,我将在 URL 中超过 41。 confirm.php 打印 41。
http://localhost/yii2-app-basic/web/site/confirm/41
但是,当我在 URL 中传递 "cfeb70c4c627167ee56d6e09b591a3ee" 或“41a”时,
http://localhost/yii2-app-basic/web/site/confirm/41a
显示错误
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.
我想向用户发送确认 ID 以确认他们的帐户。 这就是传递随机数 "cfeb70c4c627167ee56d6e09b591a3ee" 的原因。
那么,我该怎么做才能让 URL 接受字母数字参数。
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));
}
更改此行
'<controller>/<action>/<id:\d+>' => '<controller>/<action>'
至此
'<controller>/<action>/<id:[a-z0-9]+>' => '<controller>/<action>'
应该就可以了
当前规则规定 id 是一个数字 (\d+
),因此它在您的示例中不起作用。我不会修改当前规则,而是专门为这种情况添加一个规则:
'rules' => [
'site/confirm/<id:\w+>' => 'site/confirm',
'<controller>/<action>/<id:\d+>' => '<controller>/<action>'
],