Hello
I’m trying to use setFlash but I have a problem, I created a class helper. I tried it:
Helper class:
<?php
namespace yii\helpers;
use Yii;
class Utils {
function showMsgFlashes($getFlashes) {
if ($getFlashes !== null) {
foreach ($getFlashes as $tipo => $msg) {
//Gerenciando os icones de acordo com o tipo de mensagem
switch ($tipo) {
case 'danger':
$icone = '<span class="glyphicon glyphicon-remove"></span>';
break;
case 'success':
$icone = '<span class="glyphicon glyphicon-ok"></span>';
break;
case 'info':
$icone = '<span class="glyphicon glyphicon-info-sign"></span>';
break;
case 'warning':
$icone = '<span class="glyphicon glyphicon-warning-sign"></span>';
break;
default:
$icone = '<span class="glyphicon glyphicon-asterisk"></span>';
break;
}
return '<div class="alert alert-' . $tipo . '">' . $icone . ' ' . $msg . '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> </div>';
}
} else {
return false;
}
}
}
Action on controller:
public function actionAdd(){
.....
if($model->save())
return $this->redirect ('index');
}
if($qtd_model <4)
return $this->render('add',['model'=>$model]);
else
{
Yii::$app->session->setFlash('danger', 'Error to save);
return $this->render('add',['model'=>$model]);
}
}
index.php:
<?php if (Yii::$app->session->hasFlash('danger')): ?>
<?= yii\helpers\Utils::showMsgFlashes(Yii::$app->session->getAllFlashes()) ?>
<?php endif; ?>
I had this error:
How can I fix it?