2017-10-19 16 views
0

개발 중에 urlManager를 사용하여 하나의 문제에 직면했습니다.Yii2, 루스터의 선택적 매개 변수

SiteController에 "category"동작이 있습니다. 이 작업을 사용할 수 있습니다

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

요구 사항에 따라 컨트롤러 및 작업 이름에 대해서만 규칙을 설정해야합니다. 다른 모든 필드는 $ _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>', 
     ], 
    ], 
처럼 꽤 URL에 대한 일반 규칙을 사용하여 URL에 대한