Category: Yii 2
Gitignore
To exclude files from being tracked and committed to git, we can use git ignore.
Create or iedit a gitignore file with the files and folders you want to be ignored.
After that, use git bash and enter the following commands:
git rm -r –cached .
git add .
git commit -m “gitignore is now working”
Calculate the number of months between two dates in PHP?
$date1 = '2000-01-25';
$date2 = '2010-02-20';
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
You may want to include the days somewhere too, depending on whether you mean whole months or not. Hope you get the idea though.
Add Maintenance Mode to Yii 2 Application
Install this extension: https://github.com/brussens/yii2-maintenance-mode
Add to your config file:
'bootstrap' => ['log', 'maintenanceMode'],
...
'components' => [
'maintenanceMode' => [
'class' => 'brussens\maintenance\MaintenanceMode',
],
...
],
Modify according to your needs. Make sure to indicate an allowed user or role. Also create a default route for displaying maintenance mode text. Place in route.
'maintenanceMode' => [
// Component class namespace
'class' => 'brussens\maintenance\MaintenanceMode',
// Mode status
'enabled' => true,
// Route to action
'route' => 'maintenance/index',
// Allowed user names
'users' => [
'BrusSENS',
],
Add to your console config file. This is useful when you cannot access your UI:
'bootstrap' => ['log', 'maintenanceMode'],
...
'components' => [
'maintenanceMode' => [
'class' => 'brussens\maintenance\MaintenanceMode',
],
...
],
Add buttons on UI. You can use session[‘maintenance’] to store current state.
echo '<span style = "color:white;font-weight:bold;">MAINTENANCE</span><br/>';
if (Yii::$app->session['maintenance'] == NULL){
echo Html::a('ON', ['/site/enable'], ['class'=>'btn btn-primary grid-button']);
echo Html::a('OFF', ['/site/disable'], ['class'=>'btn btn-primary grid-button']);
}
if (Yii::$app->session['maintenance'] == 'OFF'){
echo Html::a('ON', ['/site/enable'], ['class'=>'btn btn-primary grid-button']);
echo Html::a('OFF', ['/site/enable'], ['class'=>'btn btn-primary grid-button', 'disabled' => 'disabled']);
}else if (Yii::$app->session['maintenance'] == 'ON') {
echo Html::a('ON', ['/site/enable'], ['class'=>'btn btn-primary grid-button', 'disabled' => 'disabled']);
echo Html::a('OFF', ['/site/disable'], ['class'=>'btn btn-primary grid-button']);
}
Add to controller. Make sure to change the state stored in maintenance when a function is called:
public function actionEnable()
{
Yii::$app->session['maintenance'] = 'ON';
Yii::$app->maintenanceMode->enable();
return $this->goHome();
}
public function actionDisable()
{
Yii::$app->session['maintenance'] = 'OFF';
Yii::$app->maintenanceMode->disable();
return $this->goHome();
}