CodeIgniter4/incoming/content_negotiation.html
2019-02-18 11:53:51 -08:00

399 lines
19 KiB
HTML

<!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>Content Negotiation &mdash; CodeIgniter4 4.0.0-alpha.5 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.5 documentation" href="../index.html"/>
<link rel="up" title="Controllers and Routing" href="index.html"/>
<link rel="next" title="HTTP Method Spoofing" href="methodspoofing.html"/>
<link rel="prev" title="IncomingRequest Class" href="incomingrequest.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"><a class="reference internal" href="message.html">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 current"><a class="current reference internal" href="#">Content Negotiation</a></li>
<li class="toctree-l2"><a class="reference internal" href="methodspoofing.html">HTTP Method Spoofing</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="../changelogs/index.html">Change Logs</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>Content Negotiation</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="content-negotiation">
<h1>Content Negotiation<a class="headerlink" href="#content-negotiation" title="Permalink to this headline"></a></h1>
<p>Content negotiation is a way to determine what type of content to return to the client based on what the client
can handle, and what the server can handle. This can be used to determine whether the client is wanting HTML or JSON
returned, whether the image should be returned as a jpg or png, what type of compression is supported and more. This
is done by analyzing four different headers which can each support multiple value options, each with their own priority.
Trying to match this up manually can be pretty challenging. CodeIgniter provides the <code class="docutils literal"><span class="pre">Negotiator</span></code> class that
can handle this for you.</p>
<div class="section" id="loading-the-class">
<h2>Loading the Class<a class="headerlink" href="#loading-the-class" title="Permalink to this headline"></a></h2>
<p>You can load an instance of the class manually through the Service class:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$negotiator</span> <span class="o">=</span> <span class="nx">\Config\Services</span><span class="o">::</span><span class="na">negotiator</span><span class="p">();</span>
</pre></div>
</div>
<p>This will grab the current request instance and automatically inject it into the Negotiator class.</p>
<p>This class does not need to be loaded on it&#8217;s own. Instead, it can be accessed through this request&#8217;s <code class="docutils literal"><span class="pre">IncomingRequest</span></code>
instance. While you cannot access it directly this way, you can easily access all of methods through the <code class="docutils literal"><span class="pre">negotiate()</span></code>
method:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$request</span><span class="o">-&gt;</span><span class="na">negotiate</span><span class="p">(</span><span class="s1">&#39;media&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;bar&#39;</span><span class="p">]);</span>
</pre></div>
</div>
<p>When accessed this way, the first parameter is the type of content you&#8217;re trying to find a match for, while the
second is an array of supported values.</p>
</div>
<div class="section" id="negotiating">
<h2>Negotiating<a class="headerlink" href="#negotiating" title="Permalink to this headline"></a></h2>
<p>In this section we will discuss the 4 types of content that can be negotiated and show how that would look using
both of the methods described above to access the negotiator.</p>
<div class="section" id="media">
<h3>Media<a class="headerlink" href="#media" title="Permalink to this headline"></a></h3>
<p>The first aspect to look at is handling &#8216;media&#8217; negotiations. These are provided by the <code class="docutils literal"><span class="pre">Accept</span></code> header and
is one of the most complex headers available. A common example is the client telling the server what format it
wants the data in. This is especially common in API&#8217;s. For example, a client might request JSON formatted data
from an API endpoint:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">GET</span> <span class="o">/</span><span class="nx">foo</span> <span class="nx">HTTP</span><span class="o">/</span><span class="mf">1.1</span>
<span class="nx">Accept</span><span class="o">:</span> <span class="nx">application</span><span class="o">/</span><span class="nx">json</span>
</pre></div>
</div>
<p>The server now needs to provide a list of what type of content it can provide. In this example, the API might
be able to return data as raw HTML, JSON, or XML. This list should be provided in order of preference:</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;application/json&#39;</span><span class="p">,</span>
<span class="s1">&#39;text/html&#39;</span><span class="p">,</span>
<span class="s1">&#39;application/xml&#39;</span>
<span class="p">];</span>
<span class="nv">$format</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">negotiate</span><span class="p">(</span><span class="s1">&#39;media&#39;</span><span class="p">,</span> <span class="nv">$supported</span><span class="p">);</span>
<span class="c1">// or</span>
<span class="nv">$format</span> <span class="o">=</span> <span class="nv">$negotiate</span><span class="o">-&gt;</span><span class="na">media</span><span class="p">(</span><span class="nv">$supported</span><span class="p">);</span>
</pre></div>
</div>
<p>In this case, both the client and the server can agree on formatting the data as JSON so &#8216;json&#8217; is returned from
the negotiate method. By default, if no match is found, the first element in the $supported array would be returned.
In some cases, though, you might need to enforce the format to be a strict match. If you pass <code class="docutils literal"><span class="pre">true</span></code> as the
final value, it will return an empty string if no match is found:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$format</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">negotiate</span><span class="p">(</span><span class="s1">&#39;media&#39;</span><span class="p">,</span> <span class="nv">$supported</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>
<span class="c1">// or</span>
<span class="nv">$format</span> <span class="o">=</span> <span class="nv">$negotiate</span><span class="o">-&gt;</span><span class="na">media</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>
</div>
<div class="section" id="language">
<h3>Language<a class="headerlink" href="#language" title="Permalink to this headline"></a></h3>
<p>Another common usage is to determine the language the content should be served in. If you are running only a single
language site, this obviously isn&#8217;t going to make much difference, but any site that can offer up multiple translations
of content will find this useful, since the browser will typically send the preferred language along in the <code class="docutils literal"><span class="pre">Accept-Language</span></code>
header:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">GET</span> <span class="o">/</span><span class="nx">foo</span> <span class="nx">HTTP</span><span class="o">/</span><span class="mf">1.1</span>
<span class="nx">Accept</span><span class="o">-</span><span class="nx">Language</span><span class="o">:</span> <span class="nx">fr</span><span class="p">;</span> <span class="nx">q</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="nx">en</span><span class="p">;</span> <span class="nx">q</span><span class="o">=</span><span class="mf">0.5</span>
</pre></div>
</div>
<p>In this example, the browser would prefer French, with a second choice of English. If your website supports English
and German you would do something like:</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;de&#39;</span>
<span class="p">];</span>
<span class="nv">$lang</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">negotiate</span><span class="p">(</span><span class="s1">&#39;language&#39;</span><span class="p">,</span> <span class="nv">$supported</span><span class="p">);</span>
<span class="c1">// or</span>
<span class="nv">$lang</span> <span class="o">=</span> <span class="nv">$negotiate</span><span class="o">-&gt;</span><span class="na">language</span><span class="p">(</span><span class="nv">$supported</span><span class="p">);</span>
</pre></div>
</div>
<p>In this example, &#8216;en&#8217; would be returned as the current language. If no match is found, it will return the first element
in the $supported array, so that should always be the preferred language.</p>
</div>
<div class="section" id="encoding">
<h3>Encoding<a class="headerlink" href="#encoding" title="Permalink to this headline"></a></h3>
<p>The <code class="docutils literal"><span class="pre">Accept-Encoding</span></code> header contains the character sets the client prefers to receive, and is used to
specify the type of compression the client supports:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">GET</span> <span class="o">/</span><span class="nx">foo</span> <span class="nx">HTTP</span><span class="o">/</span><span class="mf">1.1</span>
<span class="nx">Accept</span><span class="o">-</span><span class="nx">Encoding</span><span class="o">:</span> <span class="nx">compress</span><span class="p">,</span> <span class="nx">gzip</span>
</pre></div>
</div>
<p>Your web server will define what types of compression you can use. Some, like Apache, only support <strong>gzip</strong>:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$type</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">negotiate</span><span class="p">(</span><span class="s1">&#39;encoding&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;gzip&#39;</span><span class="p">]);</span>
<span class="c1">// or</span>
<span class="nv">$type</span> <span class="o">=</span> <span class="nv">$negotiate</span><span class="o">-&gt;</span><span class="na">encoding</span><span class="p">([</span><span class="s1">&#39;gzip&#39;</span><span class="p">]);</span>
</pre></div>
</div>
<p>See more at <a class="reference external" href="https://en.wikipedia.org/wiki/HTTP_compression">Wikipedia</a>.</p>
</div>
<div class="section" id="character-set">
<h3>Character Set<a class="headerlink" href="#character-set" title="Permalink to this headline"></a></h3>
<p>The desired character set is passed through the <code class="docutils literal"><span class="pre">Accept-Charset</span></code> header:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">GET</span> <span class="o">/</span><span class="nx">foo</span> <span class="nx">HTTP</span><span class="o">/</span><span class="mf">1.1</span>
<span class="nx">Accept</span><span class="o">-</span><span class="nx">Charset</span><span class="o">:</span> <span class="nx">utf</span><span class="o">-</span><span class="mi">16</span><span class="p">,</span> <span class="nx">utf</span><span class="o">-</span><span class="mi">8</span>
</pre></div>
</div>
<p>By default, if no matches are found, <strong>utf-8</strong> will be returned:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$charset</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">negotiate</span><span class="p">(</span><span class="s1">&#39;charset&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;utf-8&#39;</span><span class="p">]);</span>
<span class="c1">// or</span>
<span class="nv">$charset</span> <span class="o">=</span> <span class="nv">$negotiate</span><span class="o">-&gt;</span><span class="na">charset</span><span class="p">([</span><span class="s1">&#39;utf-8&#39;</span><span class="p">]);</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="methodspoofing.html" class="btn btn-neutral float-right" title="HTTP Method Spoofing" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="incomingrequest.html" class="btn btn-neutral" title="IncomingRequest Class" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2014-2019 British Columbia Institute of Technology.
Last updated on Feb 18, 2019.
</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.5',
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>