D
D
Dilik Pulatov2017-06-17 11:12:39
Yii
Dilik Pulatov, 2017-06-17 11:12:39

Multilingual site with dynamic URL in Yii2 how?

Hello!
the task is this: A multilingual site ... and a dynamic URL for each post, category and page
, that is, something like this page about (en), o-nas (ru), etc.
when the language changes, the weight of the page should change, not loss of position,
this is approximately so: there was site.com/ru/o-nas -> site.com/en/about
for this I have already done something

'urlManager' => [
  'enablePrettyUrl' => true,
  'showScriptName' => false,
  'class'=>'app\components\LangUrlManager',
  'rules' => [
    'login'=>'site/login',
    'logout'=>'site/logout',
    [
      'pattern' => '/<url1:\w+>',
      'route' => 'site/get-page',
    ],
    [
      'pattern' => '/<url1:\w+>/<url2:\w+>',
      'route' => 'site/get-page',
    ],
  ],
],

in the controller
(Url is not a helper here, but a model)
public function actionGetPage($url1,$url2=NULL){
  // debug($url1); die;
  if(isset($url1)){
    $url = Url::find()->where(['url'=>$url1,'language'=>Yii::$app->lang])->asArray()->one();
    if(!empty($url)){
      if($url['type'] == 'category'){
        return Yii::$app->runAction('/site/category/',['id'=>$url['parent_id']]);
      }
    }
  }
  header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); 
  return $this->render('error');
}

everything works fine, but the problem is that there is a url: /o_nas then everything works, but if /o-nas then gives out 404
, did I miss something? what is the problem?
By the way, if there is another option for you to solve this problem, then tell
me (sorry for the mistakes)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-06-19
@dilikpulatov

replace \w+ everywhere with [a-zA-Z0-9\-_]*
and read about regular expressions ....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question