Headers Already Sent Exception

Hi guys,
as I noticed after having upgraded yii2, each echo in Controller will 'cause Exception Headers Already Sent. Why is framework throwing this exception out?
If I send statement to be print out on screen to View as parameter from Controller in order to print out in View, everything is ganna all right. If I code like this, exeption will be thrown out.

private function Validating($model_telefon) {
    /* Diese Logik wurde gemäß folgender Gleichung der boolschen Algebra aufgebaut und mittels eines KV-Diagramm soweit als möglich vereinfacht:
      Y=(NOT a OR NOT b) AND (c OR d) OR (NOT c or NOT d) AND (a OR b)
     */
    foreach ($model_telefon as $number) {
        if ((!empty($number->vorwahl) || !empty($number->telefonnummer)) && (empty($number->id_kontakt_verwendungszweck) || empty($number->id_laenderkennung)) || (!empty($number->id_kontakt_verwendungszweck) || !empty($number->id_laenderkennung)) && (empty($number->vorwahl) || empty($number->telefonnummer))) {
            ?><?=

            Alert::widget([
                'type' => Alert::TYPE_DANGER,
                'title' => 'Eingabefehler',
                'icon' => 'glyphicon glyphicon-remove-sign',
                'body' => 'Eine Telefonnummer benötigt immer einen Verwendungszweck eine Länderkennung und eine Vorwahl',
                'showSeparator' => true,
                'delay' => false
            ]);
            return true;
        }
    }
    return false;
}

that is the expected behavior, don’t echo anything in your controllers just return what you need to output

1 Like

Previously Yii was ignoring the fact that headers were not sent but the fact was still there. Now it reports it. Erroneous output before sending headers should be fixed in your application anyway.

But what if I want to send output batch? For example, generating large XML? How to do it now?

Same as you did, just don’t send headers after content.

This can’t be correct. I don’t send any headers explicity, but I will get this exception, whenever I code an echo in controller. Controller doesn’t accept any echo any more since upgrading. My solution is like this:

  1. return instead echo in Controller
private function RenderBoerse($name, $DynamicModel) {
$ReplaceJob = $DynamicModel->job;
$url = $this->WebStringErsetzen($Jobboerse, "###", $ReplaceJob);
return Html::a("$name laden", $url, ['class' => 'btn btn-success btn-block', 'target' => '_blank', 'title' => "Load $name"]);
}
  1. Send parameter to View(extern.php) with Controller

     if ($DynamicModel->choice_Indeed) 
        $GiveBackLink[0]=$this->RenderBoerse("Indeed", $DynamicModel);
        return $this->render('extern', ['DynamicModel' => $DynamicModel,'GiveBackLink'=>$GiveBackLink]);
    
  2. echo in View, which will prevent Header Exception

 if(!empty($GiveBackLink)){
 		foreach($GiveBackLink as $link){
 			 ?><?= $link;
 		}
 }

In this example, parameter sent is an array. I don’t care a pap for it as in principle I can send whatever I want send to View using return and render() instead echoing it in Controller! In my opinion, this Exception is pretty waste as it forces me to rewrite half the code.

It’s fantastic; it solved my issue. I had been excavating for several days. Actually, Yii 2 allows echo, but only up to a certain number of bytes; if your echo grows too large, it will stop operating and transmit headers already sent error.