Search code examples
php.htaccessyii2

How to set prefix to all keys of url rules?


I want to set prefix for all keys of urlManager rules. At the moment I write rules like that:

'rules' => [
    'api' => '_public/site/index',
    'api/<controller>/<action>' => '_public/<controller>/<action>',      
     etc
]

I want to avoid copy/past-ing of api prefix to all rules. Because it could be 500 rules.

Is there a way to define somewhere a prefix in order to Yii placed it for every rule key itself?

$prefix and $prefixRoute is setting up prefix for values, but I want for keys.

Maybe this can be done by using .htaccess? But I don't know what should I write there.


Solution

  • I added a line to config file:

    $config = [
    
        'components' => [
            'request' => [
                // some code
                'baseUrl' => '/api',   // <--- this line
            ], 
        ]
    ]
    

    and it worked.

    $baseUrl - The relative URL for the application. https://www.yiiframework.com/doc/api/2.0/yii-web-request#$baseUrl-detail