Hi,
I’m trying to build some rules to translate urls of my site.
I’ve one extension (https://github.com/codemix/yii2-localeurls) wich inserts the language code in the url but this parameter is not available to build simple rules in urlManager configuration like:
'<language:it>/utenti'=>'users/index'
Following one tutorial (http://blog.neattutorials.com/yii2-routing-urlmanager/) i managed to write a class:
<?php
namespace common\components;
use Yii;
use yii\web\UrlRuleInterface;
use yii\base\Object;
class UrlLanguageRule extends Object implements UrlRuleInterface
{
public function createUrl($manager, $route, $params)
{
if ( Yii::$app->language==='it-IT'){
if ($route === 'users/index'){
return 'utenti';
}
}
return false; // this rule does not apply
}
public function parseRequest($manager, $request)
{
if ( Yii::$app->language==='it-IT'){
$url = $request->getPathInfo();
if ($url === 'utenti'){
$route = 'users/index';
$params = [];
return [$route, $params];
}
}
return false;
}
}
It function but seems a bit strange to me:in the first function ‘utenti’ is wired to ‘users/index’ in the second ‘users/index’ is wired to ‘utenti’; I have to admit I wrote this without having full understanding of what I was doing
Some suggestion for better coding?
Thanks,
Nicola