CodeIgniter4/incoming/message.html

741 lines
36 KiB
HTML
Raw Normal View History

2018-09-25 07:00:21 -07:00
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTTP Messages &mdash; CodeIgniter4 4.0.0-alpha.1 documentation</title>
<link rel="shortcut icon" href="../_static/ci-icon.ico"/>
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
<link rel="top" title="CodeIgniter4 4.0.0-alpha.1 documentation" href="../index.html"/>
<link rel="up" title="Controllers and Routing" href="index.html"/>
<link rel="next" title="Request Class" href="request.html"/>
<link rel="prev" title="Controller Filters" href="filters.html"/>
<script src="../_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../index.html" class="icon icon-home"> CodeIgniter4
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a></li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Controllers and Routing</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="controllers.html">Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="routing.html">URI Routing</a></li>
<li class="toctree-l2"><a class="reference internal" href="filters.html">Controller Filters</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">HTTP Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="request.html">Request Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="incomingrequest.html">IncomingRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="response.html">HTTP Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="api_responses.html">API Response Trait</a></li>
<li class="toctree-l2"><a class="reference internal" href="content_negotiation.html">Content Negotiation</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../license.html">The MIT License (MIT)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../changelog.html">Change Log</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">CodeIgniter4</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> &raquo;</li>
<li><a href="index.html">Controllers and Routing</a> &raquo;</li>
<li>HTTP Messages</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="http-messages">
<h1>HTTP Messages<a class="headerlink" href="#http-messages" title="Permalink to this headline"></a></h1>
<p>The Message class provides an interface to the portions of an HTTP message that are common to both
requests and responses, including the message body, protocol version, utilities for working with
the headers, and methods for handling content negotiation.</p>
<p>This class is the parent class that both the <a class="reference internal" href="request.html"><span class="doc">Request Class</span></a> and the
<a class="reference internal" href="response.html"><span class="doc">Response Class</span></a> extend from. As such, some methods, such as the content
negotiation methods, may apply only to a request or response, and not the other one, but they have
been included here to keep the header methods together.</p>
<div class="section" id="what-is-content-negotiation">
<h2>What is Content Negotiation?<a class="headerlink" href="#what-is-content-negotiation" title="Permalink to this headline"></a></h2>
<p>At it&#8217;s heart Content Negotiation is simply a part of the HTTP specification that allows a single
resource to serve more than one type of content, allowing the clients to request the type of
data that works best for them.</p>
<p>A classic example of this is a browser than cannot display PNG files can request only GIF or
JPEG images. When the getServer receives the request, it looks at the available file types the client
is requesting and selects the best match from the image formats that it supports, in this case
likely choosing a JPEG image to return.</p>
<p>This same negotiation can happen with four types of data:</p>
<ul class="simple">
<li><strong>Media/Document Type</strong> - this could be image format, or HTML vs. XML or JSON.</li>
<li><strong>Character Set</strong> - The character set the returned document should be set in. Typically is UTF-8.</li>
<li><strong>Document Encoding</strong> - Typically the type of compression used on the results.</li>
<li><strong>Document Language</strong> - For sites that support multiple languages, this helps determine which to return.</li>
</ul>
<div class="section" id="class-reference">
<h3>Class Reference<a class="headerlink" href="#class-reference" title="Permalink to this headline"></a></h3>
<dl class="class">
<dt>
<code class="descname">CodeIgniter\HTTP\Message</code></dt>
<dd><dl class="method">
<dt id="body">
<code class="descname">body</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#body" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">The current message body</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">string</td>
</tr>
</tbody>
</table>
<p>Returns the current message body, if any has been set. If not body exists, returns null:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">body</span><span class="p">();</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="setBody">
<code class="descname">setBody</code><span class="sig-paren">(</span><span class="optional">[</span><em>$str</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#setBody" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$str</strong> (<em>string</em>) &#8211; The body of the message.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">the Message instance to allow methods to be chained together.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last"><p>CodeIgniter\HTTP\Message instance.</p>
<p>Sets the body of the current request.</p>
</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="populateHeaders">
<code class="descname">populateHeaders</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#populateHeaders" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">void</td>
</tr>
</tbody>
</table>
<p>Scans and parses the headers found in the SERVER data and stores it for later access.
This is used by the <a class="reference internal" href="incomingrequest.html"><span class="doc">IncomingRequest Class</span></a> to make
the current request&#8217;s headers available.</p>
<p>The headers are any SERVER data that starts with <code class="docutils literal"><span class="pre">HTTP_</span></code>, like <code class="docutils literal"><span class="pre">HTTP_HOST</span></code>. Each message
is converted from it&#8217;s standard uppercase and underscore format to a ucwords and dash format.
The preceding <code class="docutils literal"><span class="pre">HTTP_</span></code> is removed from the string. So <code class="docutils literal"><span class="pre">HTTP_ACCEPT_LANGUAGE</span></code> becomes
<code class="docutils literal"><span class="pre">Accept-Language</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="headers">
<code class="descname">headers</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#headers" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">An array of all of the headers found.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">array</td>
</tr>
</tbody>
</table>
<p>Returns an array of all headers found or previously set.</p>
</dd></dl>
<dl class="method">
<dt id="header">
<code class="descname">header</code><span class="sig-paren">(</span><span class="optional">[</span><em>$name</em><span class="optional">[</span>, <em>$filter = null</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#header" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$name</strong> (<em>string</em>) &#8211; The name of the header you want to retrieve the value of.</li>
<li><strong>$filter</strong> (<em>int</em>) &#8211; The type of filter to apply. A list of filters can be found <a class="reference external" href="http://php.net/manual/en/filter.filters.php">here</a>.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The current value of the header. If the header has multiple values, they will be returned as an array.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string|array|null</p>
</td>
</tr>
</tbody>
</table>
<p>Allows you to retrieve the current value of a single message header. <code class="docutils literal"><span class="pre">$name</span></code> is the case-insensitive header name.
While the header is converted internally as described above, you can access the header with any type of case:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// These are all the same:</span>
<span class="nv">$message</span><span class="o">-&gt;</span><span class="na">header</span><span class="p">(</span><span class="s1">&#39;HOST&#39;</span><span class="p">);</span>
<span class="nv">$message</span><span class="o">-&gt;</span><span class="na">header</span><span class="p">(</span><span class="s1">&#39;Host&#39;</span><span class="p">);</span>
<span class="nv">$message</span><span class="o">-&gt;</span><span class="na">header</span><span class="p">(</span><span class="s1">&#39;host&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>If the header has multiple values, the values will return as an array of values. You can use the <code class="docutils literal"><span class="pre">headerLine()</span></code>
method to retrieve the values as a string:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">header</span><span class="p">(</span><span class="s1">&#39;Accept-Language&#39;</span><span class="p">);</span>
<span class="c1">// Outputs something like:</span>
<span class="p">[</span>
<span class="s1">&#39;en&#39;</span><span class="p">,</span>
<span class="s1">&#39;en-US&#39;</span>
<span class="p">]</span>
</pre></div>
</div>
<p>You can filter the header by passing a filter value in as the second parameter:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$message</span><span class="o">-&gt;</span><span class="na">header</span><span class="p">(</span><span class="s1">&#39;Document-URI&#39;</span><span class="p">,</span> <span class="nx">FILTER_SANITIZE_URL</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="headerLine">
<code class="descname">headerLine</code><span class="sig-paren">(</span><em>$name</em><span class="sig-paren">)</span><a class="headerlink" href="#headerLine" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$name</strong> (<em>string</em>) &#8211; The name of the header to retrieve.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">A string representing the header value.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Returns the value(s) of the header as a string. This method allows you to easily get a string representation
of the header values when the header has multiple values. The values are appropriately joined:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">headerLine</span><span class="p">(</span><span class="s1">&#39;Accept-Language&#39;</span><span class="p">);</span>
<span class="c1">// Outputs:</span>
<span class="nx">en</span><span class="p">,</span> <span class="nx">en</span><span class="o">-</span><span class="nx">US</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt>
<code class="descname">setHeader</code><span class="sig-paren">(</span><span class="optional">[</span><em>$name</em><span class="optional">[</span>, <em>$value</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$name</strong> (<em>string</em>) &#8211; The name of the header to set the value for.</li>
<li><strong>$value</strong> (<em>mixed</em>) &#8211; The value to set the header to.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The current message instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CodeIgniter\HTTP\Message</p>
</td>
</tr>
</tbody>
</table>
<p>Sets the value of a single header. <code class="docutils literal"><span class="pre">$name</span></code> is the case-insensitive name of the header. If the header
doesn&#8217;t already exist in the collection, it will be created. The <code class="docutils literal"><span class="pre">$value</span></code> can be either a string
or an array of strings:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$message</span><span class="o">-&gt;</span><span class="na">setHeader</span><span class="p">(</span><span class="s1">&#39;Host&#39;</span><span class="p">,</span> <span class="s1">&#39;codeigniter.com&#39;</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="removeHeader">
<code class="descname">removeHeader</code><span class="sig-paren">(</span><span class="optional">[</span><em>$name</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#removeHeader" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$name</strong> (<em>string</em>) &#8211; The name of the header to remove.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The current message instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CodeIgniter\HTTP\Message</p>
</td>
</tr>
</tbody>
</table>
<p>Removes the header from the Message. <code class="docutils literal"><span class="pre">$name</span></code> is the case-insensitive name of the header:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$message</span><span class="o">-&gt;</span><span class="na">remove</span><span class="p">(</span><span class="s1">&#39;Host&#39;</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="appendHeader">
<code class="descname">appendHeader</code><span class="sig-paren">(</span><em>[$name[, $value]])</em><span class="sig-paren">)</span><a class="headerlink" href="#appendHeader" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$name</strong> (<em>string</em>) &#8211; The name of the header to modify</li>
<li><strong>$value</strong> (<em>mixed</em>) &#8211; The value to add to the header.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The current message instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CodeIgniter\HTTP\Message</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a value to an existing header. The header must already be an array of values instead of a single string.
If it is a string then a LogicException will be thrown.</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$message</span><span class="o">-&gt;</span><span class="na">appendHeader</span><span class="p">(</span><span class="s1">&#39;Accept-Language&#39;</span><span class="p">,</span> <span class="s1">&#39;en-US; q=0.8&#39;</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="protocolVersion">
<code class="descname">protocolVersion</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#protocolVersion" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">The current HTTP protocol version</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">string</td>
</tr>
</tbody>
</table>
<p>Returns the message&#8217;s current HTTP protocol. If none has been set, will return <code class="docutils literal"><span class="pre">null</span></code>. Acceptable values
are <code class="docutils literal"><span class="pre">1.0</span></code> and <code class="docutils literal"><span class="pre">1.1</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="setProtocolVersion">
<code class="descname">setProtocolVersion</code><span class="sig-paren">(</span><em>$version</em><span class="sig-paren">)</span><a class="headerlink" href="#setProtocolVersion" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$version</strong> (<em>string</em>) &#8211; The HTTP protocol version</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The current message instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CodeIgniter\HTTP\Message</p>
</td>
</tr>
</tbody>
</table>
<p>Sets the HTTP protocol version this Message uses. Valid values are <code class="docutils literal"><span class="pre">1.0</span></code> or <code class="docutils literal"><span class="pre">1.1</span></code>:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$message</span><span class="o">-&gt;</span><span class="na">setProtocolVersion</span><span class="p">(</span><span class="s1">&#39;1.1&#39;</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="negotiateMedia">
<code class="descname">negotiateMedia</code><span class="sig-paren">(</span><em>$supported</em><span class="optional">[</span>, <em>$strictMatch=false</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#negotiateMedia" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$supported</strong> (<em>array</em>) &#8211; An array of media types the application supports</li>
<li><strong>$strictMatch</strong> (<em>bool</em>) &#8211; Whether it should force an exact match to happen.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The supported media type that best matches what is requested.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Parses the <code class="docutils literal"><span class="pre">Accept</span></code> header and compares with the application&#8217;s supported media types to determine
the best match. Returns the appropriate media type. The first parameter is an array of application supported
media types that should be compared against header values:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$supported</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;image/png&#39;</span><span class="p">,</span>
<span class="s1">&#39;image/jpg&#39;</span><span class="p">,</span>
<span class="s1">&#39;image/gif&#39;</span>
<span class="p">];</span>
<span class="nv">$imageType</span> <span class="o">=</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">negotiateMedia</span><span class="p">(</span><span class="nv">$supported</span><span class="p">);</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">$supported</span></code> array should be structured so that the application&#8217;s preferred format is the first in the
array, with the rest following in descending order of priority. If no match can be made between the header
values and the supported values, the first element of the array will be returned.</p>
<p>Per the <a class="reference external" href="http://tools.ietf.org/html/rfc7231#section-5.3">RFC</a> the match has the option of returning a
default value, like this method does, or to return an empty string. If you need to have an exact match and
would like an empty string returned instead, pass <code class="docutils literal"><span class="pre">true</span></code> as the second parameter:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// Returns empty string if no match.</span>
<span class="nv">$imageType</span> <span class="o">=</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">negotiateMedia</span><span class="p">(</span><span class="nv">$supported</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>
</pre></div>
</div>
<p>The matching process takes into account the priorities and specificity of the RFC. This means that the more
specific header values will have a higher order of precedence, unless modified by a different <code class="docutils literal"><span class="pre">q</span></code> value.
For more details, please read the <a class="reference external" href="http://tools.ietf.org/html/rfc7231#section-5.3.2">appropriate section of the RFC</a>.</p>
</dd></dl>
<dl class="method">
<dt id="negotiateCharset">
<code class="descname">negotiateCharset</code><span class="sig-paren">(</span><em>$supported</em><span class="sig-paren">)</span><a class="headerlink" href="#negotiateCharset" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$supported</strong> (<em>array</em>) &#8211; An array of character sets the application supports.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The supported character set that best matches what is required..</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>This is used identically to the <code class="docutils literal"><span class="pre">negotiateMedia()</span></code> method, except that it matches against the <code class="docutils literal"><span class="pre">Accept-Charset</span></code>
header string:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$supported</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;utf-8&#39;</span><span class="p">,</span>
<span class="s1">&#39;iso-8895-9&#39;</span>
<span class="p">];</span>
<span class="nv">$charset</span> <span class="o">=</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">negotiateCharset</span><span class="p">(</span><span class="nv">$supported</span><span class="p">);</span>
</pre></div>
</div>
<p>If no match is found, the system will default to <code class="docutils literal"><span class="pre">utf-8</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="negotiateEncoding">
<code class="descname">negotiateEncoding</code><span class="sig-paren">(</span><em>$supported</em><span class="sig-paren">)</span><a class="headerlink" href="#negotiateEncoding" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$supported</strong> (<em>array</em>) &#8211; An array of character encodings the application supports.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The supported character set that best matches what is required..</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Determines the best match between the application-supported values and the <code class="docutils literal"><span class="pre">Accept-Encoding</span></code> header value.
If no match is found, will return the first element of the <code class="docutils literal"><span class="pre">$supported</span></code> array:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$supported</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;gzip&#39;</span><span class="p">,</span>
<span class="s1">&#39;compress&#39;</span>
<span class="p">];</span>
<span class="nv">$encoding</span> <span class="o">=</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">negotiateEncoding</span><span class="p">(</span><span class="nv">$supported</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="negotiateLanguage">
<code class="descname">negotiateLanguage</code><span class="sig-paren">(</span><em>$supported</em><span class="sig-paren">)</span><a class="headerlink" href="#negotiateLanguage" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>$supported</strong> (<em>array</em>) &#8211; An array of languages the application supports.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">The supported language that best matches what is required..</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Determines the best match between the application-supported languages and the <code class="docutils literal"><span class="pre">Accept-Language</span></code> header value.
If no match is found, will return the first element of the <code class="docutils literal"><span class="pre">$supported</span></code> array:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$supported</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;en&#39;</span><span class="p">,</span>
<span class="s1">&#39;fr&#39;</span><span class="p">,</span>
<span class="s1">&#39;x-pig-latin&#39;</span>
<span class="p">];</span>
<span class="nv">$language</span> <span class="o">=</span> <span class="nv">$message</span><span class="o">-&gt;</span><span class="na">negotiateLanguage</span><span class="p">(</span><span class="nv">$supported</span><span class="p">);</span>
</pre></div>
</div>
<p>More information about the language tags are available in <a class="reference external" href="https://www.ietf.org/rfc/rfc1766.txt">RFC 1766</a>.</p>
</dd></dl>
</dd></dl>
</div>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="request.html" class="btn btn-neutral float-right" title="Request Class" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="filters.html" class="btn btn-neutral" title="Controller Filters" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2014-2018 British Columbia Institute of Technology.
Last updated on Sep 25, 2018.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../',
VERSION:'4.0.0-alpha.1',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: false,
SOURCELINK_SUFFIX: ''
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>