1

Hello I am not an expert in Yii2 and would appreciate any help, We want to change our default module,
Our logic:

site implements a use of wildcard domain, https://example.com,
we implement a bootstrap component to Identify a use of a "subdomain" in the url I.E. https://sub.example.com,

$config = [
'id' => 'basic',
'name' => 'exapmle',
'basePath' => dirname(__DIR__),
'bootstrap' => [
    'log',
    'devlogin',
    'app\components\SubBootstrap', #this is the bootstrap component we use
    'app\components\ThemeBootstrap',
],...

now we would have liked to use the same logic to change the default module to a new "submodule" but we can't use the bootstrap because it happens after the default module has been applied.

obviously we can have an explicit url call for the module I.E.

'modules' => [
    'sub'=>[
        'class' => 'app\modules\sub\Module',
    ],...

but that means that the url would look like https://somesub.example.com/sub/ which is undesirable.

thank you very much

3
  • does the name of the module depend on the subdomain or do you want / on every subdomain to call the same module? Commented Jan 15, 2019 at 11:10
  • Depends on what you want, you might either set yiiframework.com/doc/api/2.0/… to 'sub/default/index' or configure pretty URLs: yiiframework.com/doc/guide/2.0/en/… Commented Jan 15, 2019 at 11:12
  • @cebe we want the same module every time. in addition it's not only nice url it's also best practice to keep logic of sub separated from the rest of the code base. Thanks! Commented Jan 15, 2019 at 11:22

2 Answers 2

1

In your case, what you can do is override the UrlManager component and manually adjust the path to reflect the module that you want to envoke behind the scenes.

So your code would look something like this:

<?php

namespace app\components;

use Yii;

class UrlManager extends \yii\web\UrlManager
{
    public function parseRequest($request)
    {
        if (!empty(Yii::$app->sub)) {
            $pathInfo = $request->pathInfo;
            $moduleIds = array_keys(Yii::$app->modules);
            $inModule = false;
            foreach ($moduleIds as $moduleId) {
                if (preg_match("/^{$moduleId}/", $pathInfo)) {
                    $inModule = true;
                    break;
                }
            }

            if (!$inModule) {
                $pathInfo = 'sub/' . $pathInfo;
                $request->setPathInfo($pathInfo);
            }
        }

        return parent::parseRequest($request);
    }
}

and then in config/web.php:

'urlManager' => [
    'class' => 'app\components\UrlManager',
    ...
],
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Chaim really worked like a charm exactly that way.
0

You don't need to change the module configuration. You need to change web-server path to this module and architech the UrlManager rules; https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing

Bootstraping yii module it's only some way to load it before another components. https://www.yiiframework.com/doc/guide/2.0/en/runtime-bootstrapping

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.