Site building and maintenance

Blog. Yii. Tips for Yii. Сhapter 1

Search

There are many unclear points by developing any product, to find an answer for them you use Google or follow the advice given by your colleagues, or read manuals.

We would like to help you and to save your time, in case you have the same problems.

  1. The most important are functions of auto-finishing Yii code in IDE:

    For example, NetBeans:

    Click the right mouse button at the name of the project, choose ‘Properties’ in the context menu (at the bottom).
    Then, in the window find a section Php include path, click the button ‘Add folder’ and add the Yii framework root path.

    netbeans include path

  2. Replacement for common usage of arrays $_REQUEST, $_POST, $_GET:

    $request = Yii::app()->request;

    Instead of $param = isset($_REQUEST['id']) ? $_REQUEST['id'] : 1; you should use $param = $request->getParam('id', 1);

    Instead of $param = isset($_GET['id']) ? $_GET['id'] : null; - $param = $request->getQuery('id');

    Instead of $param = isset($_POST['id']) ? $_POST['id'] : 1; - $param = $request->getPost('id', 1);

  3. More useful methods:

    Yii::app()->getRequest()->getUrl() - http://monoray.net/forum/index?var=val

    Yii::app()->getRequest()->getHostInfo() - http://monoray.net/forum/index?var=val

    Yii::app()->getRequest()->getPathInfo() - http://monoray.net/forum/index?var=val

    Yii::app()->getRequest()->getRequestUri() - http://monoray.net/forum/index?var=val

    Yii::app()->getRequest()->getQueryString() - http://monoray.net/forum/index?var=val

  4. Your message by incorrect output value - attribute message:

     

    1. public function rules() {  
    2.     return array(  
    3.         array('username, password''required''message' => 'Please, fill out the field "{attribute}"'),  
    4.     );  
    5. }  

     

  5. The captcha is not updated by incorrect input value – try the following:

     

    1. public function actions() {  
    2.     return array(  
    3.         'captcha' => array(  
    4.             'class' => 'CCaptchaAction',  
    5.             'backColor' => 0xFFFFFF,  
    6.             'testLimit'=> 1, //   
    7.         ),  
    8.     );  
    9. }  

     

    'testLimit' is set for the value 3 by default. So the code is updated only after the third incorrect input value.

  6. If you need to update a part of the page by ajax request and upload all the scripts use:

     

    1. $this->renderPartial('index'array(  
    2.         'criteria' => $criteria, ), false, true);  

     

    Just the last parameter, which is set as true, says that it is necessary to output the page with all its scripts.

  7. Menu is based on Cmenu, but instead of text (label) an image is needed:

    There are two ways:

    1. to add the path to the image in 'label' and install 'encodeLabel' in the value false:

       

      1. $adminMenuItems = array(  
      2.         array('label' => '<img src="'.Yii::app()->request->baseUrl.'/images/adminmenu/manage_ads.png" />',   
      3.                 'url'=>array('/apartments/backend/main/admin'),   
      4.                 'linkOptions'=>array('title' => Yii::t('module_apartments''Manage apartments'))  
      5.             )  
      6. );  
      7.   
      8. $this->widget('zii.widgets.CMenu'array(  
      9.             'items'=>$adminMenuItems,                     
      10.             'encodeLabel' => false,   
      11. ));  

       

    2. Use template:

       

      1. $adminMenuItems = array(  
      2.         array('label'=>'',   
      3.                 'url'=>array('/quicksearch/main/index'),   
      4.                 'template' => '<img src="'.Yii::app()->request->baseUrl.'/images/adminmenu/manage_ads.png" />'  
      5.         )  
      6. );  
      7.   
      8. $this->widget('zii.widgets.CMenu'array(  
      9.     'items'=>$adminMenuItems,  
      10. ));  

       

  8. Sometimes it is necessary to use the settings (from the configuration file /protected/config/main.php) of the application in Javascript code.

    To get the value of any key in the array params it is necessary to add the following code:

     

    1. $globalConfig = CJavaScript::encode(Yii::app()->params->toArray());   
    2. Yii::app()->clientScript->registerScript('globalConfig'"var globalConfig = ".$globalConfig.";", CClientScript::POS_HEAD);  

     

    So we can output the value that we need:

     

    1. Yii::app()->clientScript->registerScript('testConfig'' 
    2.     if(globalConfig){ 
    3.             alert(globalConfig["languages"]); 
    4.     } 
    5. ', CClientScript::POS_END);  

     

Discuss the article in the forum