在开发过程中,我遇到了urlManager的一个问题。
我的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'
你想帮我解决这个问题吗?
答案 0 :(得分:0)
根据您的要求,您只需为Controller和操作名称设置规则。所有其他字段都通过$ _GET传递。并且您可以使用Yii::$app->getRequest()->getQueryParam('param')
方法获取它们。
对于您的网址,您可以使用常规规则,例如
'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>',
],
],