没有不值得去解决的问题,也没有不值得去学习的技术!

How to Keep a Role Parameter in All Links When Users Switch Roles in the Yii2 Backend

Use it as follows in the view, and the generated link works as expected

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.

I want all links on this entry URL to automatically have the request parameter role=distributor appended, but currently they do not
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
<?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.

PHP
        '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.

On this entry URL, all links now automatically have the request parameter role=distributor appended. This works as expected
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.

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

7. When generating a link, manually add a flag ‘_skip_role’ => true, and then check for it in createUrl().

PHP
<?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.

Use it as follows in the view, and the generated link works as expected
Figure 4
PHP
<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>


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系