E
E
EVOSandru62016-02-22 13:10:54
Yii
EVOSandru6, 2016-02-22 13:10:54

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>',
    ],
]

DTenantUrlManager :
class DTenantUrlManager extends UrlManager
{
    /** Переопределение createUrl */
    public function createUrl($route, $params = [], $ampersand = '&')
    {
        $url = parent::createUrl($route, $params, $ampersand);
        return TenantHelper::addTenantToUrl($url);
    }
}

DTenantHttpRequest (defined for request in config)
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());
    }
}

TenantHelper
/** Процесс фирмы в адресной строке */
    /** Тут затираем все лишнее и тут будут отрабатывать правила по шаблонам из 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;
        /** Вывод значения из сессии */
    }

The getFirmID method is kind of muddy( When following links , DTenantHttpRequest::getRequestUri gives the desired one - /tenant/2/hotels/view/id/87 , but I can't hook any get parameters from this line. Probably because there are slashes instead of ' ? ' and ' & '
params :
return
[
    ...
    'tenant' =>
    [
        'default'   => 1,
    ],
    ...
]

<?php echo Yii::app()->createUrl('blog/view', ['id' => 1]); ?>

On the incoming page, it gives the desired /tenant/2/blog/view/id/1 , but the default action of the default controller opens.
Please tell me what to fix in order to be able to access tenant via GET or some other way. And why am I flying into the default action?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question