Site building and maintenance

Blog. Yii. Tips for Yii. Chapter 13

Search

Tips for Yii. Chapter 13.

  1. Cookie and subdomains.

    If your website has multiple subdomains, it will be better to store user authorization both on your primary domain and on subdomains.

    For example:the primary domain is http://domain_site.com. The subdomains are http://msk.domain_site.com and http://spb.domain_site.com.

    User logged on http://domain_site.com, but he willbe the "guest" not an "authorized user" on http://msk.domain_site.com.

    If you want to avoid such misunderstanding, you need to find the configuration file (for example: protected\config\main.php) and just add to "components" section the following code:

    1. 'session' => array(  
    2.     'cookieParams' => array('domain' => '.domain_site.com'),  
    3. ),  

    and

    1. 'user'=>array(  
    2.     // enable cookie-based authentication  
    3.     'allowAutoLogin'=>true,  
    4.     'identityCookie' => array('domain' => '.domain_site.com'),  
    5. ),  

    In summary, it has to look like this:

    1. ...  
    2. // application components  
    3. 'components'=>array(  
    4.     ...  
    5.     'session' => array(  
    6.         'cookieParams' => array('domain' => '.domain_site.com'),  
    7.     ),  
    8.     ...  
    9.     'user'=>array(  
    10.         // enable cookie-based authentication  
    11.         'allowAutoLogin'=>true,  
    12.         'identityCookie' => array('domain' => '.domain_site.com'),  
    13.     ),  
    14.     ...  
    15. ),  
    16. ...  

    After these changes, you have to clear cookie in your browser plus the script's cache.

    The cache of the script is usually stored in protected/runtime/cache.

    However, you can specify a folder for cache's storage. This can be done in the configuration file.

    Write this:

    1. 'runtimePath' => Yii::getPathOfAlias('system') . '/../my_cache_folder/',  

    After:

    1. 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',  

    After that, the cache will be stored from the "root" of the site (where index.php is) in "my_cache_folder".

     

  2. Sorting according to the distance from the current object.

    Task: By viewing a listing using Open Real Estate CMS, output "Similar listings" in order of distance increasing from the current location of the object.

    Solution:

    The location coordinates of each object (listing) are stored in the table {prefix}_apartment. The fieldslat and lng.

    "viewSimilarAds" method is responsible for structuring of similar listings facility ($criteria) in the file protected\modules\similarads\components\SimilarAdsWidget.php.

    In this file instead of:

    1. $criteria->order = 't.id ASC';  

    Write this:

    1. $tmp[] = '*';  
    2. $tmp[] = new CDbExpression('( 3959 * acos( cos( radians('.$data->lat.') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('.$data->lng.') ) + sin( radians('.$data->lat.') ) * sin( radians( lat ) ) ) ) AS distance');  
    3. $criteria->select = $tmp;  
    4. $criteria->having = 'distance < 40';  
    5. $criteria->order = 'distance';  

    "3959" is the radius of the earth in miles and "40" is the maximum distance in miles.

     

    You can also withdraw the distance from the viewed object.

    Find this file protected\modules\apartments\models\Apartment.php and after:

    1. public $title;  

    add:

    1. public $distance;  

    Then in protected\modules\similarads\views\widgetSimilarAds_list.php, after:

    1. echo '<div class="similar-price">'.tt('Price from''apartments').': '.$item->getPrettyPrice().'</div>';  

    add:

    1. echo '<div class="similar-descr">distance from this object of: '.round($item->distance, 1).' miles</div>';  

     

Discuss the article in the forum