1. Currently, the entry link to switch the role to distributor is on the backend homepage: https://console.apply.local/?role=distributor
2. I want all links on this entry URL to automatically have the request parameter role=distributor appended, but currently they do not. See Figure 1.

3. Create a custom UrlManager component and override the createUrl method to automatically append the current role parameter when the role parameter in the URL is distributor or service.
<?php
namespace management\components;
use Yii;
class UrlManager extends \yii\web\UrlManager
{
public function createUrl($params): string
{
$role = Yii::$app->request->get('role', 'management');
if (in_array($role, ['distributor', 'service'])) {
$params['role'] = $role;
}
return parent::createUrl($params);
}
}
4. Configure the application to use the custom UrlManager by replacing the default urlManager in the backend configuration file.
'urlManager' => [
'class' => 'management\components\UrlManager',
],
5. On this entry URL, all links now automatically have the request parameter role=distributor appended. This works as expected. See Figure 2.

6. However, there are a few links that should not have the request parameter role=distributor appended. These exactly need to be excluded. See Figure 3.

7. When generating a link, manually add a flag ‘_skip_role’ => true, and then check for it in createUrl().
<?php
namespace management\components;
use Yii;
class UrlManager extends \yii\web\UrlManager
{
public function createUrl($params): string
{
if (!isset($params['_skip_role'])) {
// 仅当 role 为 distributor|service 时,才支持参数 role
$role = Yii::$app->request->get('role', 'management');
if (in_array($role, ['distributor', 'service']) && !isset($params['role'])) {
$params['role'] = $role;
}
} else {
unset($params['_skip_role']); // 清除这个临时参数,避免出现在 URL 中
}
return parent::createUrl($params);
}
}
8. Use it as follows in the view, and the generated link works as expected. See Figure 4.

<div class="col-6 mb-3 text-right">
<a class="btn btn-warning btn-rounded" href="<?php echo Url::toRoute(['/', '_skip_role' => true]); ?>">管理 <?php echo $eventCount['management'];?></a> <!-- / -->
<a class="btn btn-danger btn-rounded" href="<?php echo Url::toRoute(['/', 'role' => 'distributor']); ?>">联络 <?php echo $eventCount['distributor'];?></a> <!-- /?role=distributor -->
<a class="btn btn-purple btn-rounded" href="<?php echo Url::toRoute(['/', 'role' => 'service']); ?>">接待 <?php echo $eventCount['service'];?></a> <!-- /?role=service -->
</div>
