Search code examples
phpyii2yii2-basic-appyii-url-manager

Yii2 Basic Template UrlManager rules for web/index


I've made an REST API in the Yii2 Basic Template. For this REST API i've changed the web.php. This is what I have added:

'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            //basic/web
            //basic/web/users
            ['class' => 'yii\rest\UrlRule', 'controller' => ['user', 'car']],
            //basic/web/countries
            //Id is een string, vandaar de tokens
            ['class' => 'yii\rest\UrlRule', 'controller' => 'country', 'tokens' => ['{id}' => '<id:\\w+>']],
        ],
    ],

This works fine. But now I can't acces the index page from the web folder anymore. What rule do I need to add to access the pages from the web folder again?


Solution

  • Try this,

    'rules' => [
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' =>    '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ['class' => 'yii\rest\UrlRule', 'controller' => ['user', 'car']],
            //basic/web/countries
            //Id is een string, vandaar de tokens
            ['class' => 'yii\rest\UrlRule', 'controller' => 'country', 'tokens' => ['{id}' => '<id:\\w+>']],
    ]