UrlManager - add Parameter automatically

Has anyone a hint for me how I can solve follow problem:

I have different modules in my WebApp (Module 1, Module 2, Module 3, …) When the user use “Module 1”, the background color change, the menu change, etc.

I store the current module as session.

In some cases, the user open “Modul 1” and a new Browser Tab with “Modul 2”. Then he goes back in Modul 1 and klick on a Link – now the Menu, background, etc. is changed to “Modul 2”.

So my idea is to add a parameter in the url which identify the current module and add this parameter automatically to every url.

Can I do this with rules in the UrlManager? Or any other ideas? Thanks!

yii\base\Module::$params lets you specify arbitrary key value pairs that you can associate with a particular module. So if the theme settings are attached to the module (and not to the user session), its probably the most easy way to store these settings there.


Edit:
I just saw that you probably meant that the module ID is stored in the user session, and not the theme settings. In that case you should definitively reference the module id in the URL. But since that happens by default, I am wondering why that is not working? Could explain some more details (how do URL’s look like, how to you currently resolve a module if the module ID comes from the session, …)

Hallo and thank you Coksnuss for your answer!

I saw the naming is not ideal. My Module is not equale with the module of Yii2 sorry!

“Model 1” means a Function block in our system.

So for example, invoicing is one function and hast items for invoicing and a backgroundcolor orange

“Model 2” is the second function “CRM” and has the backgroundcolor blue. And so on, thanks!

I see. So the term “module” is actually a Controller in Yii? In that case you could easily set the desired theme settings for the current request in the init() method of the controller.

I am sorry, but I still don’t see that the theme settings are related to the user session. Are they?

Thats also possible, but it requires you to explicitly specify URL rules (see https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing#default-parameter-values).

Thanks for your help, but this doesn’t solve my problem.
So I’ve written an own UrlManager called “UrlMan” and initialized this component (dirty implementation for tests,but will help for other user maybe):

class UrlMan extends \yii\web\UrlManager

{

public function init()
{
    parent::init();

    // ... initialization after configuration is applied
}

public function createUrl($params)
{

    $params = (array) $params;
    $anchor = isset($params['#']) ? '#' . $params['#'] : '';
    $modno = 1;
    if(isset($_REQUEST['NEWislimMod']))
    {
        $modno = $_REQUEST['NEWislimMod'];

    }else if(isset($_REQUEST['islimMod']))
    {
        $modno = $_REQUEST['islimMod'];
    }
    $anchor = '&islimMod='.$modno . $anchor;
    unset($params['#'], $params[$this->routeParam]);

    $route = trim($params[0], '/');
    unset($params[0]);

    $baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();

    if ($this->enablePrettyUrl) {
        $cacheKey = $route . '?';
        foreach ($params as $key => $value) {
            if ($value !== null) {
                $cacheKey .= $key . '&';
            }
        }

        $url = $this->getUrlFromCache($cacheKey, $route, $params);
        if ($url === false) {
            /* @var $rule UrlRule */
            foreach ($this->rules as $rule) {
                if (in_array($rule, $this->_ruleCache[$cacheKey], true)) {
                    // avoid redundant calls of `UrlRule::createUrl()` for rules checked in `getUrlFromCache()`
                    // @see https://github.com/yiisoft/yii2/issues/14094
                    continue;
                }
                $url = $rule->createUrl($this, $route, $params);
                if ($this->canBeCached($rule)) {
                    $this->setRuleToCache($cacheKey, $rule);
                }
                if ($url !== false) {
                    break;
                }
            }
        }

        if ($url !== false) {
            if (strpos($url, '://') !== false) {
                if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) {
                    return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor;
                }

                return $url . $baseUrl . $anchor;
            } elseif (strncmp($url, '//', 2) === 0) {
                if ($baseUrl !== '' && ($pos = strpos($url, '/', 2)) !== false) {
                    return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor;
                }

                return $url . $baseUrl . $anchor;
            }

            $url = ltrim($url, '/');
            return "$baseUrl/{$url}{$anchor}";
        }

        if ($this->suffix !== null) {
            $route .= $this->suffix;
        }
        if (!empty($params) && ($query = http_build_query($params)) !== '') {
            $route .= '?' . $query;
        }

        $route = ltrim($route, '/');
        return "$baseUrl/{$route}{$anchor}";
    }

    $url = "$baseUrl?{$this->routeParam}=" . urlencode($route);
    if (!empty($params) && ($query = http_build_query($params)) !== '') {
        $url .= '&' . $query;
    }

    return $url . $anchor;
}

}