YII Information
Installing framework:
- 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
- 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:
-
Create Model
-
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'=>array('register','captcha'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','index','view'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>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'=>$dataProvider,
'itemView'=>'_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:
$userid=Yii::app()->user->getid();
$criteria=new CDbCriteria(array(
'condition'=>'user_id='.$userid,
));
$dataProvider=new CActiveDataProvider('Project', array(
'pagination'=>array(
'pageSize'=>Yii::app()->params['postsPerPage'],
),
'criteria'=>$criteria,
));
$this->render('index',array(
'dataProvider'=>$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);