URL Routing gives 404 when ID is send

Hi

In one of my Yii2 projects a problem has occurred with URL Routing. I want to use this pattern ‘<controller:\w+>/<action:\w+>/<id:\d+>’=>’<controller>/<action>’ so users can enter URLs like domain.com/controller/action/id. The exact same pattern works fine on other Yii2 projects but not in this one. The real URL i want to resolve is

http://domain.com/engineering-reports/view/123

where 123 is the ID of the record to show. It is a pretty basic setup. One thing that I find wierd is that the URL http://domain.com/engineering-reports/view routes just fine and renders as expected. It is when I add the ID param in the end of the URL that a 404-error is returned. Any clues what could cause this?

Here are some syntax that possibly could be useful

config/web.php


$config = [

    'id' => 'CMD',

    'language' => "en_us",

    'basePath' => dirname(__DIR__),

    'defaultRoute' => 'site',

    'bootstrap' => ['log'],

    'components' => [

        'request' => [

            'cookieValidationKey' => 'veryverysercret',

        ],

        'cache' => [

            'class' => 'yii\caching\FileCache',

        ],

        'user' => [

            'identityClass' => 'app\models\User',

            'enableAutoLogin' => true,

            'loginUrl'=>['/login']

        ],

        'errorHandler' => [

            'errorAction' => 'site/error',

        ],

        'mailer' => [

            'class' => 'yii\swiftmailer\Mailer',

            'useFileTransport' => true,

        ],

        'log' => [

            'traceLevel' => YII_DEBUG ? 3 : 0,

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

        'urlManager' => [

            'showScriptName' => false,

            'enablePrettyUrl' => true,

            'rules' => [

                'login' => 'site/login',

                'logout' => 'site/logout',

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

            ],

        ],

        'db' => require(__DIR__ . '/db.php'),

        'assetManager' => [

            'converter' => [

                'class' => 'yii\web\AssetConverter',

                'commands' => [

                    // 'less' => ['css', 'lessc {from} {to} --no-color']

                    'less' => ['css', 'nodejs "node_modules/less/bin/lessc" {from} "web/assets/css/"{to} --no-color'],

                ],

            ],

            'forceCopy' => true

        ]

    ],

    'as beforeRequest' => [

        'class' => 'yii\filters\AccessControl',

        'rules' => [

            [

                'actions' => ['login', 'error'],

                'allow' => true,

            ],

            [

                'allow' => true,

                'roles' => ['@'],

            ],

        ],

    ],

    'params' => $params,

];

web/.htaccess


RewriteEngine on

 

# If a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d


# Otherwise forward it to index.php

RewriteRule . index.php

I found out what was wrong. The URL-rule

‘<controller:\w+>/<action:\w+>/<id:\d+>’=>’<controller>/<action>’

did not allow for dashes in URL. Since there was a dash in the URL we changed the rule to

‘<controller:[\w\-]+>/<action:[\w\-]+>/<id:[\d]+>’ => ‘<controller>/<action>’

Then it works fine.