grandadmiral
grandadmiral

Reputation: 15

Add language to url in Yii2

I have following url for example:

http://mywebsite.com/testing/test

And I want it to be something like this depending on the current language:

http://mywebsite.com/en/testing/test

I have language stored in: Yii::$app->language

I tried to modify my URLManager (/config/web.php), but it doesn't work for me.

'urlManager' => [
  'class' => 'yii\web\UrlManager',
  'showScriptName' => false,
  'enablePrettyUrl' => true,
  'rules' => [
    '/login' => 'site/login',
    '/logout' => 'site/logout',
    Yii::$app->language.'/'.'<controller:\w+>s'=>'<controller>/index',
    Yii::$app->language.'/'.'<controller:\w+>/<id:\d+>/<title:\w+>'=>'<controller>/view',
    Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
    Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',
  ],
],

Can You help me?

Upvotes: 1

Views: 713

Answers (1)

Chris Weit
Chris Weit

Reputation: 70

I found an extension. Even if you don't want to use it, you can look into it’s code to get an idea how it works. (Changing patterns / routing / etc.)

https://github.com/codemix/yii2-localeurls

Upvotes: 2

Related Questions