Passing Default Parameter for a given controller/action

I have a simple scenario where I need to pass a default “fn” parameter/value when none is passed. My rules are as below:

            'rules' => [
                [
                'pattern' => 'post/index',
                'route' => 'post/index',
                'defaults' => ['fn' => 'a'],
                ]
            ],

When I call /post/index my expectation is that /post/index?fn=a is passed. The above gives an error: Missing required parameters: fn

I think at least the format 'pattern' => 'post/index/<fn>' should work.

https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing#default-parameter-values

After adding 'pattern' => 'trtask/index/<fn>',

  1. post/index - works and defaults fn=a
  2. post/index?fn=b - does not work as it still defaults to fn=a
  3. post/ - does not work produces Missing required parameters: fn
  4. post/?fn=b - works

I would like 2 & 3 also to work.

Put the default value in the Controller action declaration…

public function actionIndex($fn=a){
.......
}
1 Like

That worked! Thanks!