phpyii2yii2-advanced-app

yii2 createUrl is duplicating the route


i'm building multi lingual site with English and Arabic

url for English

url for Arabic

I want to switch language from any page exactly to the same page of the other language so i made code like below.

   $route = Yii::$app->controller->route;
   $params = $_GET;
   array_unshift($params, '/'.$route);

  <?php if(Yii::$app->language == 'ar'){ ?>
  <?= Html::a('English', [Yii::$app->urlManager->createUrl($params), 'language'=>'en']); ?>
  <?php }else{?>
  <?= Html::a('Arabic', [Yii::$app->urlManager->createUrl($params), 'language'=>'ar']); } ?>

and my url generating like below

/multi/backend/web/en/multi/backend/web/ar/site/index?val=hii&net=good

English

don't know what is wrong?

I'm using this for language management.

please check my main.php under backend/config

            <?php
            $params = array_merge(
                require(__DIR__ . '/../../common/config/params.php'),
                require(__DIR__ . '/../../common/config/params-local.php'),
                require(__DIR__ . '/params.php'),
                require(__DIR__ . '/params-local.php')
            );

            return [
                'id' => 'app-backend',
                'basePath' => dirname(__DIR__),
                'controllerNamespace' => 'backend\controllers',
                'language' => 'en',
                'sourceLanguage' => 'en_UK',
                'bootstrap' => ['log'],
                'modules' => [],
                'components' => [
                    'user' => [
                        'identityClass' => 'common\models\User',
                        'enableAutoLogin' => true,
                    ],
                    'log' => [
                        'traceLevel' => YII_DEBUG ? 3 : 0,
                        'targets' => [
                            [
                                'class' => 'yii\log\FileTarget',
                                'levels' => ['error', 'warning'],
                            ],
                        ],
                    ],
                    'errorHandler' => [
                        'errorAction' => 'site/error',
                    ],
                    'i18n' => [
                        'translations' => [
                            'app' => [
                                'class' => 'yii\i18n\DbMessageSource',
                                'sourceLanguage' => 'en_UK',
                            ],
                        ],
                     ],
                    'urlManager' => [
                        'class' => 'codemix\localeurls\UrlManager',
                        'languages' => ['en', 'ar'],
                        'enableDefaultLanguageUrlCode' => false,
                        'enablePrettyUrl' => true,
                        'showScriptName' => false,
                        'rules' => [
                        ],
                    ],
                ],
                'params' => $params,
            ];

Solution

  • after lots of trying ..i found a solution.. now its worked for me.

        $route = Yii::$app->controller->route;
        if(Yii::$app->language == 'ar'){
            $_GET['language'] = 'en';
        }else{
            $_GET['language'] = 'ar';
        }
        $params = $_GET;
        array_unshift($params, '/'.$route);
    

      <?php if(Yii::$app->language == 'ar'){ ?>
      <?= Html::a('English', Yii::$app->urlManager->createUrl($params)); ?>
      <?php }else{?>
      <?= Html::a('Arabic', Yii::$app->urlManager->createUrl($params)); } ?>
    

    its working for the url like

    http://localhost/multi/backend/web/site/index?page=2&per-page=6

    and

    http://localhost/multi/backend/web/site/index