Kokizzu
Kokizzu

Reputation: 26898

Yii urlManager unlimited parameters

is there is a way in yii to make parameters unlimited

for example, i have module /admin/

'urlManager'=>array(
  'urlFormat'=>'path',
    'showScriptName'=>false,
    'caseSensitive'=>false,
    'rules'=>array(
      '<controller:\w+>/<id:\d+>'=>'<controller>/view',
      '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
      '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',                
      'admin/<controller:\w+>/<action:\w+>/<id:\d+>' => 'admin/<controller>/<action>',
      'admin/<controller:\w+>/<action:\w+>'=>'admin/<controller>/<action>',          
    ),
  ),

and within admin module i need that every action could have unlimited parameters, for example:

 /admin/anycontroller/anyaction/anything
 /admin/anycontroller/anyaction/anything/anything2
 /admin/anycontroller/anyaction/anything/anything2/anything3
 /admin/anycontroller/anyaction/anything/anything2/anything3/anything4 
 ... and so on

should i define it one by one on the rules? or there is shortcut to do this?

and how to catch it on the controller action?

Upvotes: 3

Views: 4092

Answers (1)

bool.dev
bool.dev

Reputation: 17478

There is a shortcut:

'admin/<controller:\w+>/<action:\w+>/*'=>'admin/<controller>/<action>'

i.e append the rule with a /*

Since this is a more general rule, which can catch a lot of urls, it would be better to have it at the bottom, or atleast after any specific rules, i.e:

// ... other specific rules ...
'<controller:\w+>/<action:\w+>/<id:\w+>'=>'<controller>/<action>', // specifically for id
// ... other specific rules ...
'admin/<controller:\w+>/<action:\w+>/*'=>'admin/<controller>/<action>'

For your case:

'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'admin/<controller:\w+>/<action:\w+>/<id:\d+>' => 'admin/<controller>/<action>',
'admin/<controller:\w+>/<action:\w+>'=>'admin/<controller>/<action>',
'admin/<controller:\w+>/<action:\w+>/*'=>'admin/<controller>/<action>'

To catch it in the action, just don't specify any parameters for the action, like so:

public function actionSomething() {
    // instead use $_GET
    $params=$_GET;

}

But it should also work with the definition that you already have: public function actionAnyAction($id=0,$type='',$type2='')

Upvotes: 5

Related Questions