3

I have submitted one module in Magento Marketplace. They gave me 2 errors in my code.

Use of echo language construct is discouraged.

Use of exit language construct is discouraged.

For echo, I have added /* @noEscape */ before echo.

But for exit, I don't know, how to solve error.

Code piece from Observer.php

           if ($maintenancePageBody != '') {
                if ($redirectPage == 0) {
                    /* @noEscape */ echo $blankpageMsg;
                } else if ($redirectPage == 1) {
                    /* @noEscape */ echo $maintenancePage;
                } else if ($redirectPage == 2 && $customUrl != '') {
                    header('Location:' . $customUrl);
                    exit;
                } else {
                    /* @noEscape */ echo $maintenancePage;
                }
            }
           // exit; /* my code */
           $this->getResponse()->setBody(); // I used this line instead of `exit`

Magento Marketplace error is,

Use of exit language construct is discouraged.

File: Model/Observer.php Line: 717 Column: 17 Source: MEQP1.Security.LanguageConstruct.ExitUsage

EDIT

I submitted extension again. echo error still occurs. $maintenancePage variable has HTML content.

Would anyone please help me, what should I do for echo and exit ?

  • Is there any need of exit? You can remove it and use return instead. – Jaimin Sutariya Mar 2 '17 at 5:48
  • Thanks Jaimin for reply. I have tried return, but my code stop to work. – Prince Mar 2 '17 at 6:04
2

if in controller's execute method , echo $a;

then please replace

echo $a 

to

return $this->getResponse()->setBody($a);
1

Here is official set of rules and sniffs for PHP CodeSniffer tool - https://github.com/magento/marketplace-eqp

Run it and fix all highlighted issues!

More details - http://docs.magento.com/marketplace/user_guide/extensions/review-code-validation.html

  • Simple, yet very effective advice here. This is definitely the correct answer. – df2k2 Sep 11 '19 at 7:01
0
Use of die language construct is discouraged.

Use of exit language construct is discouraged.

Recommendation: setBody() response object method should be used.

Use of echo language construct is discouraged.

Use of print language construct is discouraged.

Recommendation: Architecture of the extension should be changed in order to avoid usage of echo, header etc. in classes, consider using setBody() method of the response object.

For more see here.

  • Thanks Arunendra. I have read that but I don't know how to use setBody(). I am new bee in Magento. Would you please help me by my code or any other example ? – Prince Mar 2 '17 at 6:05
  • @Prince this might help you on setBody() : stackoverflow.com/questions/4441526/… – Arunendra Mar 2 '17 at 6:09
  • I have tried that example but I got Fatal error: Uncaught Error: Call to undefined method Vendor\Module\Model\Observer::getResponse() – Prince Mar 3 '17 at 6:28
  • use this code Mage::app()->getResponse()->setBody('Some Response'); – Arunendra Mar 3 '17 at 7:16
  • Module is in Magento 2. And your comment example is in Magento. – Prince Mar 3 '17 at 7:28
0

If you are getting download content from inside loop, then use like this

while ($buffer = strip_tags($ioAdapter->streamRead()))
{
   $myContent .= $buffer;
}

//Set content to CSV
$this->getResponse()->setBody($myContent);
return;

And replace exit; with return;

This resolved my problem. Hope this helps.

0

Follow FireBear advice regarding the codesniffing.

I can tell you, though, that you aren't really on the correct track. What you are trying to do doesn't make much sense to Magento2.

First, it seems you are using an Observer by this:

Code piece from Observer.php

If you are not using an Observer to intercept an event, then you should probably change the name of your file from Observer.php. Very misleading.

For the most part, you cannot echo any output except in template files, those are the .phtml files.

Next, if you are truly observing an event, you'll definitely NOT want to cause any interruption of the code execution with header(), nor will you want to echo any output since Observers are not meant for that.

Events and Observers: https://devdocs.magento.com/guides/v2.2/extension-dev-guide/events-and-observers.html

Observer Best practices: https://devdocs.magento.com/guides/v2.2/ext-best-practices/extension-coding/observers-bp.html

If you do want to run some code and return output for a template or frontend rendering, then you'll likely implement the Magento\Framework\App\ActionInterface

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.