Yii2,rounte中的可选参数

Yii2,rounte中的可选参数,yii,controller,yii2,rules,yii-url-manager,Yii,Controller,Yii2,Rules,Yii Url Manager,在开发过程中,我遇到了urlManager的一个问题 我有带有“类别”操作的SiteController 可用于此操作的所有可能组合: id, city, location = null id, city = null, location id, city = null, location = null id = null, city = null, location = null id = null, city = null, location 我不知道如何在UrlManager中编写规则,

在开发过程中,我遇到了urlManager的一个问题

我有带有“类别”操作的SiteController

可用于此操作的所有可能组合:

id, city, location = null
id, city = null, location
id, city = null, location = null
id = null, city = null, location = null
id = null, city = null, location
我不知道如何在UrlManager中编写规则,之后我将在按下链接后在变量中获得以下值:

<h4><?= Html::a('ID City', ['/site/category', 'id' => 'barbershop', 'city' => 'Praha'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = 'barbershop'
city = 'Praha'
location = ''

<h4><?= Html::a('ID Location', ['/site/category', 'id' => 'barbershop', 'location' => '23,65'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = 'barbershop'
city = ''
location = '23,65'

<h4><?= Html::a('ID', ['/site/category', 'id' => 'barbershop'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = 'barbershop'
city = ''
location = ''

<h4><?= Html::a('City', ['/site/category', 'city' => 'Praha'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = ''
city = 'Praha'
location = ''

<h4><?= Html::a('Location', ['/site/category', 'location' => '14,23'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = ''
city = ''
location = '14,23'

“类别”操作的返回值:
id=‘理发店’
城市=‘普拉哈’
位置=“”
“类别”操作的返回值:
id=‘理发店’
城市=“”
位置='23,65'
“类别”操作的返回值:
id=‘理发店’
城市=“”
位置=“”
“类别”操作的返回值:
id=“”
城市=‘普拉哈’
位置=“”
“类别”操作的返回值:
id=“”
城市=“”
位置='14,23'

您想帮我解决这个问题吗?

根据您的要求,您只需要为控制器和操作名称设置规则。所有其他字段都通过$\u GET传递。您可以使用
Yii::$app->getRequest()->getQueryParam('param')
方法获取它们

对于你的url,你可以使用普通的规则,比如

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ],
    ],
'urlManager'=>[
“enablePrettyUrl”=>true,
'showScriptName'=>false,
“规则”=>[
“/”=>“/视图”,
'//' => '/',
'/' => '/',
],
],
'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ],
    ],