Multiple Variables to be passed in URL - Yii2

2019-08-15 17:33发布

问题:

I want to generate URL's that can handle multiple parameters as follows.

www.mysite.com/index.php/controller/param1/param2/param3/param4/mySlug 

But still be able to be flexible and pass over less information

www.mysite.com/index.php/controller/param1/parama/my_slug_2 

We could assume there's always a slug at the end if that makes this easier. Is there anything in the Yii2 urlManager I can implement this.

回答1:

Just configure your rule with default values of your params like this:

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            [
                'pattern' => 'test/<param1:\d+>/<param2:\d+>/<param3:\d+>/<param4:\d+>/<slug>',
                'route' => 'site/test',
                'defaults' => [
                    'param1' => null,
                    'param2' => null,
                    'param3' => null,
                    'param4' => null,
                ]
            ]
        ]
    ],

All the parameters, that have default value are not required and may be skipped. Here are the examples of behaviour:

http://yii2.local/test/slug:

skipped.array (size=5)
  'param1' => null
  'param2' => null
  'param3' => null
  'param4' => null
  'slug' => string 'slug' (length=4)  

http://yii2.local/test/2/4/slug

array (size=5)
  'param1' => string '2' (length=1)
  'param2' => string '4' (length=1)
  'param3' => null
  'param4' => null
  'slug' => string 'slug' (length=4)


回答2:

Horrible that this is a 2 year question, and the answer is so simple.

The idea is that for some reason when the ActiveForm is not well configure for the GET requests, specifically on the action, it will cause this weird effect.

Explanation: The reason for the above behavior is due to a unique validation needed in Yii's Html::beginForm method. The query parameters in the action are ignored for GET method so Yii's Html::beginForm uses hidden inputs for each GET parameter to add them back. These hidden inputs gets stacked over the last URI call with every submission. «Yii Forum»

This can get solved by simply adding a well configured action to the $form = ActiveForm.

On Yii2:

$form = ActiveForm::begin([
                'method' => 'get',
                'action' => Url::to(['/search']),
            ]);

This will get rid of this horrible effect. For more info on this read the Yii forum

I hope this helps any other, that runs on the problem.



回答3:

This works a bit nicer, as it allows for any number of optional params, great if for example you're creating a category tree that may be x levels deep (you also don't need slug if not needed):

In the config:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        [
            'pattern' => 'test/<params:.*>/<slug>',
            'route' => 'site/test',
            'encodeParams' => false,
            'defaults' => [
                'params' => null,
            ]
        ]
    ]
],

You can then get an array of all possible params in the controller:

public function actionTest($params = null, $slug) {     

    if (!empty($params)) {
        $params = explode('/', $params);
    }
    // do whatever...

}