Answer the question
In order to leave comments, you need to log in
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',
],
],
],
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');
}
Answer the question
In order to leave comments, you need to log in
replace \w+ everywhere with [a-zA-Z0-9\-_]*
and read about regular expressions ....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question