Yii2 disable access on the correct part of the routing rule

In web.phpI have the following code:

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
                'calc' => 'site/calc',
        ),
    ],

And I want to allow users access to example.com/calc, but not to example.com/site/calc. How can I do this with less effort? In other words, now both options work " site/calc" and " calc", and I want to disable " site/calc".

+4
source share
1 answer

Try adding enableStrictParsing to your UrlManager configuration as follows

 'urlManager' => [
    'class' => 'yii\web\UrlManager',
    // Disable index.php
    'showScriptName' => false,
    // Disable r= routes
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'rules' => array(
            'calc' => 'site/calc',
    ),
],

In this case, if there is no rule matching the request, it is considered bad.

+1
source

Source: https://habr.com/ru/post/1611752/


All Articles