Skip to main content
format the code
Source Link
Manashvi Birla
  • 8.9k
  • 9
  • 28
  • 53

A lot of Magento tutorials are using echo to output controller text. However, using echo can cause weird problems, like the infamous “Headers already sent” error.

I understand that the right way to return response is by using setBody()setBody(), eg $this->getResponse()->setBody($text);, but for some cases I prefer using echo for convenience (returning by parts vs returning all at once).

So my question is - is there a "safe" Magento way to use echo in controllers? or is it a big no-no and should never be used?

PS I can use collect data myself and just call setBody() at the end (eg do something like "$ret .= ..." instead of echo), but in this case an uncaught error will not give me any output, while using echo will at least show me some of the output, up until the error. I'm talking about debug pages here so partial is still very useful :)

Thanks,

A lot of Magento tutorials are using echo to output controller text. However, using echo can cause weird problems, like the infamous “Headers already sent” error.

I understand that the right way to return response is by using setBody(), eg $this->getResponse()->setBody($text);, but for some cases I prefer using echo for convenience (returning by parts vs returning all at once).

So my question is - is there a "safe" Magento way to use echo in controllers? or is it a big no-no and should never be used?

PS I can use collect data myself and just call setBody() at the end (eg do something like "$ret .= ..." instead of echo), but in this case an uncaught error will not give me any output, while using echo will at least show me some of the output, up until the error. I'm talking about debug pages here so partial is still very useful :)

Thanks,

A lot of Magento tutorials are using echo to output controller text. However, using echo can cause weird problems, like the infamous “Headers already sent” error.

I understand that the right way to return response is by using setBody(), eg $this->getResponse()->setBody($text);, but for some cases I prefer using echo for convenience (returning by parts vs returning all at once).

So my question is - is there a "safe" Magento way to use echo in controllers? or is it a big no-no and should never be used?

PS I can use collect data myself and just call setBody() at the end (eg do something like "$ret .= ..." instead of echo), but in this case an uncaught error will not give me any output, while using echo will at least show me some of the output, up until the error. I'm talking about debug pages here so partial is still very useful :)

Source Link
Ronen Ness
  • 587
  • 6
  • 19

Is it ever ok to use "echo" in controllers? What's the Magento way?

A lot of Magento tutorials are using echo to output controller text. However, using echo can cause weird problems, like the infamous “Headers already sent” error.

I understand that the right way to return response is by using setBody(), eg $this->getResponse()->setBody($text);, but for some cases I prefer using echo for convenience (returning by parts vs returning all at once).

So my question is - is there a "safe" Magento way to use echo in controllers? or is it a big no-no and should never be used?

PS I can use collect data myself and just call setBody() at the end (eg do something like "$ret .= ..." instead of echo), but in this case an uncaught error will not give me any output, while using echo will at least show me some of the output, up until the error. I'm talking about debug pages here so partial is still very useful :)

Thanks,