Important information

YII Information

Installing framework:

  1. Setup environmental variables:

Go to My Computer=>Properties=>Advanced=>Environmental Variables=>Add New

In variable name type any name

In variable value typein the path to CLI file.e.g., D:\wamp\www\bin\php\php5.2.9

  1. Go To CMD:

Move to the folder where you have placed your framework.

D:

Cd wamp\www\yiiframework

Cd framework

Yiic webapp D:\wamp\www\projname

This will create a folder in wamp for your project.

 Path for using gii:

http://hostname/path/to/index.php?r=gii

 Path for login

http://localhost/projname/index.php?r=site/login

 While using GII:

  1. Create Model

  2. Create CRUD

The default user enteries i.e. demo/demo and admin/admin are in components/userIdentity.php file.

 Creating an anchor link:

Put following code in view files:

<?php echo CHtml::link(’<b>’.’ Register Now!’.’</b>’, $this->createAbsoluteUrl(‘user/register’)); ?>

 Creating an anchor link for a static page:

<?php echo $form->checkBox($model,‘terms’); ?> I agree to <?php echo CHtml::link(’<b>’.’ terms and conditions.’.’</b>’, $this->createAbsoluteUrl(‘site/page’, array (‘view’=>‘terms’))); ?>

 For captcha or any registration function you need to add corresponding access control in the controller as well like:

public function accessRules()

{


	return array(


		array('allow',  // allow all users to perform 'index' and 'view' actions


			'actions'=&gt;array('register','captcha'),


			'users'=&gt;array('*'),


		),


        


		array('allow', // allow authenticated user to perform 'create' and 'update' actions


			'actions'=&gt;array('create','update','index','view'),


			'users'=&gt;array('@'),


		),


		array('allow', // allow admin user to perform 'admin' and 'delete' actions


			'actions'=&gt;array('admin','delete'),


			'users'=&gt;array('admin'),


		),


		array('deny',  // deny all users


			'users'=&gt;array('*'),


		),


	);


}

For adding captcha on any form add the code in the view file and then captcha code( public function action) in the controller(For Reference refer to sites controller and contact view).

*After changing fieldname in database, the corresponding "itemView" specified in corresponding view also needs to be changed.For e.g.,

It is specified in the form:

<?php $this->widget(‘zii.widgets.CListView’, array(

'dataProvider'=&gt;&#036;dataProvider,


'itemView'=&gt;'_view',

)); ?>

in the index file. In this case _view needs to be updated as well for corresponding field name.

 if a new field is added in the database, then, changes must be made to model, views, controller that make use of the corresponding table.

 CVarDumper::dump(varname,depth,true);

For printing variable value.

 Using Scenarios list for validations:

Define scenarios in model file and then specify the scenario n while creating an object on the model class in the controller:

Model file:

array(‘password,passwordagain’, ‘required’,‘on’=>‘register’),

Controller file, inside function:

$model=new User(‘register’);

 Using own conditions inside dataprovider:

    &#036;userid=Yii::app()-&gt;user-&gt;getid(); 


     &#036;criteria=new CDbCriteria(array(


            'condition'=&gt;'user_id='.&#036;userid,  


     )); 


     &#036;dataProvider=new CActiveDataProvider('Project', array(


            'pagination'=&gt;array(


                    'pageSize'=&gt;Yii::app()-&gt;params['postsPerPage'],


            ),


            'criteria'=&gt;&#036;criteria,


      ));


         &#036;this-&gt;render('index',array(


        'dataProvider'=&gt;&#036;dataProvider,


      ));

Creating Multi-step form :

//Creating image links

<?php echo CHtml::link(’<img src="’.Yii::app()->baseUrl . ‘/images/postproject.jpg" alt=“View” />’, array(“project/create”)); ?>

//Setting image path for upload

$rootPath = Yii::getPathOfAlias(‘webroot’);

//setting image path for display

Yii::app()->baseUrl

//Using templates for mail Body

$body = $this->renderPartial(’/mailtemplates/forgotpassword_mail_tpl’, array(‘username’ => $record->username,‘password’=>$random_number), true);