use yii\web\Session; $session = Yii::$app->session; // start session $session->open(); // close session $session->close(); // destroys session. $session->destroy(); //store value in session $session['user_id'] = 2; echo $session['user_id']; //result will be 2 // check if a session is set if ($session->isActive) //unset session variable unset($_SESSION['user_id']);
Yii2 Framework
Wednesday, May 2, 2018
yii2 session handling
yii2 kartik datepicker
Step 1: Install below extension from github
$ php composer.phar require kartik-v/yii2-widget-datepicker "@dev"
Step 2: Use below code in your view file
use kartik\date\DatePicker; // usage without model echo ''; echo DatePicker::widget([ 'name' => 'birth_date', 'value' => date('d-M-Y'), 'options' => ['placeholder' => 'Select birth date'], 'pluginOptions' => [ 'format' => 'dd-M-yyyy', ] ]);
Tuesday, May 1, 2018
yii2 bootstrap modal popup
For implementing this you need to call default modal of Yii2 and begin and end modal is must
In step 2 you need to add javascipt for call the modal on button click.
In step 3 you need to create a action which render the data into your view.
How to add bootstrap modal in yii2
Add in your view://php code Modal::begin([ 'header' => 'your header', 'id'=>'modal', size'=>'modal-lg', ]); echo 'Add in your controller/action:'; Modal::end(); Html::button('Button Name', ['value' => Url::to(['url/to']), 'title' => 'Title', 'class' => 'showModalButton btn btn-success']); //javascript code $('button').click(function(){ $('#modal').modal('show') .find('#modalContent') .load($(this).attr('value')); })
//php code if(Yii::$app->request->isAjax){ return $this->renderAjax('your-view-file', [ 'data' => $data, ]);
yii2 generate random string
You can easily generate random string using security class.
Yii2 generate unique number
$string = Yii::$app->security->generateRandomString(12); print_r($string); //result JCt-5GeUlR8q
yii2 gridview custom column
In this example you can see Name attribute using as custom column.
Yii2 gridview custom column
GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'id', 'username', 'created_at', [ 'label' => 'Name', 'value' => function ($model) { return $model->user->first_name; } ], ['class' => 'yii\grid\ActionColumn'], ], ]);
Monday, April 30, 2018
yii2 custom validator
In this example create a function checkStatus() and apply in the rules like below:
If the status is false then it will display the error message.
Yii2 custom validation rules
//model file public function rules() { return [ [['status'], 'checkStatus'], // other rules ]; } public function checkStatus($attribute, $params) { if($this->status == false){ $this->addError($attribute, 'Status cannot be false'); } }
yii2 compare validation
In this example you can see password and password_again attributes comparing using compare and compareAttribute.
Another example is validates if price is greater than or equal to 5000 using the compare and compareAttribute.
Yii2 Compare validation Rules
[ // validates "password" attribute equals to "password_again" ['password', 'compare'], // same as above but with explicitly specifying the attribute to compare with ['password', 'compare', 'compareAttribute' => 'password_again'], // validates if price is greater than or equal to 5000 ['price', 'compare', 'compareValue' => 5000, 'operator' => '>=', 'type' => 'number'], ]
Recent Update
yii2 session handling
use yii\web\Session; $session = Yii::$app->session; // start session $session->open(); // close session $session->close(); ...
Most Search
-
You can easily generate random string using security class. Yii2 generate unique number $string = Yii::$app->security->generateR...
-
For implementing this you need to call default modal of Yii2 and begin and end modal is must In step 2 you need to add javascipt for cal...
-
kartik Select2 onChange and Ajax Call In this example you can see a function userAlert() and this will call after typing in select2 text...