sammela.com header image 2

The OpenCart Response Class

November 13th, 2009 · No Comments · news, opencart

The OpenCart Response class has a simple job. It outputs a page of HTML to a client browser.

But it does a few other things related to that function as well, including the following:

  • It compresses output data in accordance with how the OpenCart installation is configured and the client browser capabilitiles.
  • It maintains an array of header data and sends the header data, prior to outputting the page HTML

Here is a complete list of the public methods of the Response class:

  • addHeader($key, $value)
  • Adds a header to an internal keyed list of headers. When the output is constructed the headers are all transmitted first.

  • removeHeader($key)
  • Since the internal header list is keyed, a particular header can be removed using this method.

  • redirect($url)
  • Immediately redirects to $url. For debugging and maintenance purposes, it is much better to redirect using this method, rather than sprinkling redirects throughout code.

  • setOutput($output, $level = 0)
  • Sets the HTML to be output by the Response class. If the optional $level parameter is not false (i.e. non-zero and not set to FALSE), the Response class will attempt to compress the output sent to the browser.

  • output()
  • Send headers if not already sent. Compress the output if $level is true. Echo the output.

Try It

It is simple to test the Responseclass, as shown below:

1
2
3
$rsp = new response();
$rsp->setOutput("This is a test of the Response class with compression enabled",1);
$rsp->output();

Note On Compression

I had not done any PHP output compression before I looked at the OpenCart Response class. The compress method accomplishes this by first determining the ability of the browser to uncompress.

The value of $_SERVER['HTTP_ACCEPT_ENCODING'] will be set to ‘gzip’ if the browser is capable of accepting gzip encoding.

The value of $_SERVER['HTTP_ACCEPT_ENCODING'] will be set to ‘x-gzip’ if the browser is capable of accepting x-gzip encoding.

If the browser is not capable of accepting either type of compression, the compress method simply returns uncompressed data.

If the browser is capable of accepting either type of compression, the compress method then checks to see if the server has the proper library installed to do the compression and further it checks that no headers have already been set. If both these conditions are true, the compress method compresses the output data and returns it.

Tags:

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment