Site building and maintenance

Blog. Yii. Tips for Yii. Chapter 9

Search

Tips for Yii. Chapter 9.

  1. Sending and saving a file for downloading with the help of the sendFile function.

    Properly speaking, an example:

    1. Yii::app()->request->sendFile('fileName.txt'file_get_contents(Yii::getPathOfAlias('webroot.uploads').DIRECTORY_SEPARATOR.'test.txt'));  

    The first parameter what is sent into the function by sending a file to a user is a file’s name.

    The second parameter is a file’s content.

    As the third parameter you can manually specify a file’s mimeType, that is ‘null’ as default (it is set automatically).

    The fourth parameter can be set as a flag - true/false. It will be used to terminate the running of the application or not after calling the function sendFile, ‘true’ as default.

    If a profiler is enabled (logging), for instance a yii-debug-toolbar extension, add a calling of a disableProfiler method before calling a sendFile function (this method is available only the Open Real Estate CMS):

    1. Controller::disableProfiler();  

    (you can see the code of the method in the protected\components\Controller.php file)

    It is necessary to prevent profile’s notes from being added to a file’s content by downloading.

     

  2. Sending and saving large files for downloading with the help of the xsendFile function.

    A site of the web developer is https://tn123.org/mod_xsendfile/

    Its using in the Yii:

    1. Yii::app()->request->xSendFile(Yii::getPathOfAlias('webroot.uploads').DIRECTORY_SEPARATOR.'test.txt'array(    
    2.      'saveName'=>'fileName.txt',    
    3.      'mimeType'=>'text/plain',    
    4.      'terminate'=>true,    
    5. ));   

    The function is very similar to Yii::app()->request->sendFile (besides all the advantages it is clearly shown what parameters and keys for what functions are responsible, so I will not write about it).

    Installation and setting of xSendFile for XAMPP:

    • Download the Windows binaries from the developer's site;
    • Copy a mod_xsendfile.so file from the downloaded archive into apache\modules;
    • Add the following note to apache\conf\httpd.conf:
      1. LoadModule xsendfile_module modules/mod_xsendfile.so  
    • Add the following two lines into the apache\conf\httpd.conf file or apache\conf\extra\httpd-vhosts.conf file (it depends on the settings) after DocumentRoot "C:/xampp/htdocs":
      1. XSendFile On  
      2. XSendFilePath "C:/xampp/htdocs"  

     

  3. Multilanguages in Yii. Search for untranslated words/phrases/sentences.

    An onMissingTranslation event will help us.

    In config/main.php:

    1. ...    
    2. 'messages'=>array(    
    3.     'forceTranslation'=>true,    
    4.     'onMissingTranslation' => array('CustomEventHandler''handleMissingTranslation'),    
    5. ),    
    6. ...   

    The first value of the array is a class name, and the second value is a method name in this class.

    The code of the CustomEventHandler class in the protected\components\CustomEventHandler.php file is the following:

    1. <?php    
    2. class CustomEventHandler {    
    3.     static function handleMissingTranslation($event) {    
    4.         // to the database you can add the information that there is no translation for the message ($event->message) from the category ($event->category) in the language ($event->language)  
    5.         // or you can send an e-mail to the administrator about the missing of translation  
    6.             
    7.         $sql = "INSERT INTO {{not_translate_message}} (category, message, language)  
    8.                         VALUES (:category, :message, :language)";    
    9.                             
    10.         Yii::app()->db->createCommand($sql)    
    11.             ->bindValue(':category'$event->category, PDO::PARAM_STR)    
    12.             ->bindValue(':message'$event->message, PDO::PARAM_STR)    
    13.             ->bindValue(':language,'$event->language, PDO::PARAM_STR)    
    14.             ->execute();    
    15.     }    
    16. }   

     

Discuss the article in the forum