Yii2,rounte 中的可选参数
Yii2, optional parameter in the rounte
在开发过程中,我遇到了一个urlManager的问题。
我有带 "category" 操作的 SiteController。
public function actionCategory($id = null, $city = null, $location = null)
{
echo $id;
echo $city;
echo $location;
}
可与此操作一起使用的所有可能组合:
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'
你愿意帮我解决这个问题吗?
根据您的要求,您只需为控制器和操作名称设置规则。所有其他字段都通过 $_GET 传递。您可以使用 Yii::$app->getRequest()->getQueryParam('param')
方法获取它们。
对于你来说url你可以使用正常的规则来漂亮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的问题。
我有带 "category" 操作的 SiteController。
public function actionCategory($id = null, $city = null, $location = null)
{
echo $id;
echo $city;
echo $location;
}
可与此操作一起使用的所有可能组合:
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'
你愿意帮我解决这个问题吗?
根据您的要求,您只需为控制器和操作名称设置规则。所有其他字段都通过 $_GET 传递。您可以使用 Yii::$app->getRequest()->getQueryParam('param')
方法获取它们。
对于你来说url你可以使用正常的规则来漂亮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>',
],
],