Destroyer.0211
Destroyer.0211

Reputation: 113

How I add route rule from different controller or view or file by Urlmanager class in yii2

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>',
            ),
    ],

Upvotes: 1

Views: 510

Answers (1)

Grey
Grey

Reputation: 363

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>',
        );

Upvotes: 0

Related Questions