Best way to show Flash messages in Yii

Last updated:

This is, for me, the best way to show Flash Messages on a view in Yii:

(Just place it at the top of every view you have to make sure the Flash Messages get displayed correctly.)

(I'm pretty sure there is a way to make Yii do that automatically,i.e. include that in every view you have, but I haven't figured it out yet.)

$flashMessages = Yii::app()->user->getFlashes();
if ($flashMessages) {
    echo '<ul class="flashes">';
    foreach($flashMessages as $key => $message) {
        echo '<li><div class="flash-' . $key . '">' . $message . "</div></li>\n";
    echo '</ul>';

Dialogue & Discussion