1

I want to add common url rules from a different files in YII2. How I array merge in return Urlmanager array. I study about this

 getUrlManager()->addRules

but don't know I use it.

  'urlManager' => [
        'class' => 'yii\web\urlManager',
        'baseUrl' => $paths['baseUrl'].'/backend',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
    ],
Destroyer.0211
  • 113
  • 1
  • 3
  • 13

1 Answers1

0

If i understand you, you want to store url rules in another file? Then you can just require file, like.

'urlManager' => [
    'class' => 'yii\web\urlManager',
    'baseUrl' => $paths['baseUrl'].'/backend',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
        'rules' => require('rules.php'),
    ],

And rules.php should be like

<?php
return array(
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        );
Grey
  • 357
  • 3
  • 12