7

I have the following Magento code :

protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream')
{
    $response = $this->getResponse();
    $response->setHeader('HTTP/1.1 200 OK','');
    $response->setHeader('Pragma', 'public', true);
    $response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
    $response->setHeader('Content-Disposition', 'attachment; filename='.$fileName);
    $response->setHeader('Last-Modified', date('r'));
    $response->setHeader('Accept-Ranges', 'bytes');
    $response->setHeader('Content-Length', strlen($content));
    $response->setHeader('Content-type', $contentType);
    $response->setBody($content);
    $response->sendResponse();
    die;
}

when I try to validate my code I have this error: Use of die language construct is discouraged.

What I can do or what is the best way to replace the die function.

  • For completeness of the information: Where/how are you validating your code? – 7ochem Apr 13 '18 at 8:27
8

It depends, where this code is:

  1. In a controller or post-dispatch observer?

    Then there is no need to send the response and exit by yourself, Magento will take care of that. So instead of

    $response->sendResponse();
    die;
    

    return from the action or observer:

    return;
    

    If your intention was to prevent post dispatch observers (in a controller), you can do that by additionally calling:

    $this->setFlag('', self::FLAG_NO_POST_DISPATCH, 1);
    
  2. In a pre-dispatch observer?

    Similar to above, you will want to prevent the actual controller to be dispatched:

    $controller->setFlag('', self::FLAG_NO_DISPATCH, 1);
    

    And maybe the post dispatch observers as well:

    $controller->setFlag('', self::FLAG_NO_POST_DISPATCH, 1);
    
  3. Anywhere else?

    That's a problem. Dealing with request and response is job of the controller (and observers attached to controller related events). So the best way would be to figure out how to move that code.

| improve this answer | |

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.