Answer the question
In order to leave comments, you need to log in
How to store tenant in Saas project on Yii1?
Good afternoon?
It's better in the global , session , or constantly keep in GET (in addition, additional rules in the urlManager)
I liked the example of storing the language as in this article: www.elisdn.ru/blog/39/yii-based-multilanguage-site...
But There Yii::app()->language is used , where the value can be stored. Unfortunately, you cannot save values in params properties from config/main.php .
If not global and not session, then it is necessary to constantly store it in $_GET['tenant'] , in this case, there is no way out without adding rules, as I understand it. In the case of a language, it could be stored in Yii::app()->language
Here's what I came up with:
urlManager :
'urlManager' =>return
[
'class' => 'DTenantUrlManager',
'urlFormat' => 'path',
'showScriptName' => false,
'rules' =>
[
'/' => 'hotels/index',
'<tenant:\d+>/<action:(login|logout|registration)>' => 'users/<action>',
'<tenant:\d+>/<action:(contact)>' => 'site/<action>',
'<tenant:\d+>/<controller:\w+>/<id:\d+>' => '<controller>/view',
'<tenant:\d+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<tenant:\d+>/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>/<id>',
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
],
]
class DTenantUrlManager extends UrlManager
{
/** Переопределение createUrl */
public function createUrl($route, $params = [], $ampersand = '&')
{
$url = parent::createUrl($route, $params, $ampersand);
return TenantHelper::addTenantToUrl($url);
}
}
class DTenantHttpRequest extends CHttpRequest
{
private $_requestUri;
public function getRequestUri()
{
/** Если запрос не получен */
if($this->_requestUri == null)
{
$this->_requestUri = TenantHelper::processTenantToUrl(parent::getRequestUri());
}
}
/** показать оригинальный URL адрес */
public function getOriginalUrl()
{
return $this->getOriginalUrl();
}
/** показать оригинальный URI адрес */
public function getOriginalRequestUri()
{
return TenantHelper::addTenantToUri($this->getRequestUri());
}
}
/** Процесс фирмы в адресной строке */
/** Тут затираем все лишнее и тут будут отрабатывать правила по шаблонам из UrlManager */
public static function processTenantToUrl($url)
{
$domains = explode('/', ltrim($url,'/'));
$url = '/' . implode('/',$domains);
echo $url;
return $url;
}
/** Функция добавления фирмы в налачло строки для построения url */
public static function addTenantToUrl($url)
{
$domains = explode('/', ltrim($url, '/'));
if(isset($_GET['tenant']))
$tenant = (int)$_GET['tenant'];
else
$tenant = self::getDefaultTenant();
array_unshift($domains, $tenant);
array_unshift($domains, 'tenant');
$url = '/' . implode('/',$domains);
return $url;
}
/** Фирма по умолчанию */
public static function getDefaultTenant()
{
return Yii::app()->params['tenant']['default'];
}
/** Получить id фирмы */
public static function getFirmID()
{
// echo 'getFirmID: ' .Yii::app()->request->getParam('tenant');
/** Если значение пришло из get */
if(isset($_GET['tenant']))
{
$tenant = Yii::app()->request->getParam('tenant');
}
else
{
$tenant = $_GET[1];
}
return $tenant;
/** Вывод значения из сессии */
}
return
[
...
'tenant' =>
[
'default' => 1,
],
...
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question