3

In Joomla 3.x versions I use JResponse to get and set the body.

function onAfterRender() {
          $content = JResponse::getBody();
          ...
    }

But in Joomla4 it returns Class 'JResponse' not found. So how to get the body in Joomla4?

Thanks

1

1 Answer 1

4

According to the documentation, JApplicationWeb seems to be alive and well in Joomla4.

since 2.5.0

note As of 4.0 this class will be abstract

  • The getters:

      JApplicationWeb::getBody()
    

Or

    JFactory::getApplication()->getBody()
  • The setters:

      JApplicationWeb::setBody()
    

Or

    JFactory::getApplication()->setBody()

Relevant references and examples:

3
  • 1
    JFactory::getApplication()->getBody() is definitely recommended way. Commented Oct 25, 2019 at 15:40
  • Tell me more. Better yet, tell everyone in an answer please. You know better than me. Why is one better than the other? @George Commented Oct 25, 2019 at 15:43
  • JFactory::getApplication returns an instance of JApplicationCms which is a subclass of JApplicationWeb. As a result rather than calling a non-static method in JApplicationWeb statically - grab the main application object and call the function in it's non-static intended way :) Commented Mar 20, 2020 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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