mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
826 lines
52 KiB
HTML
826 lines
52 KiB
HTML
|
||
|
||
<!DOCTYPE html>
|
||
<html class="writer-html5" lang="en" >
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
|
||
<title>API Response Trait — CodeIgniter 4.1.2 documentation</title>
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
|
||
|
||
<link rel="shortcut icon" href="../_static/favicon.ico"/>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<!--[if lt IE 9]>
|
||
<script src="../_static/js/html5shiv.min.js"></script>
|
||
<![endif]-->
|
||
|
||
|
||
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/js/citheme.js"></script>
|
||
<script src="../_static/js/carbon.js"></script>
|
||
|
||
<script type="text/javascript" src="../_static/js/theme.js"></script>
|
||
|
||
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="next" title="Localization" href="localization.html" />
|
||
<link rel="prev" title="HTTP Responses" href="response.html" />
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav">
|
||
|
||
|
||
<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" style="background: #DD4814" >
|
||
|
||
|
||
|
||
<a href="../index.html">
|
||
|
||
|
||
|
||
|
||
<img src="../_static/ci-logo-text.png" class="logo" alt="Logo"/>
|
||
|
||
</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><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working With HTTP Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul class="current">
|
||
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Building Responses</a><ul class="current">
|
||
<li class="toctree-l2"><a class="reference internal" href="views.html">Views</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="view_cells.html">View Cells</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="view_renderer.html">View Renderer</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="view_layouts.html">View Layouts</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="view_parser.html">View Parser</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="table.html">HTML Table Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="response.html">HTTP Responses</a></li>
|
||
<li class="toctree-l2 current"><a class="current reference internal" href="#">API Response Trait</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="localization.html">Localization</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="alternative_php.html">Alternate PHP Syntax for View Files</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/events.html">Database Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Manipulation with Database Forge</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/cookies.html">Cookies</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/curlrequest.html">CURLRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Service</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html">Working with Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/honeypot.html">Honeypot Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/images.html">Image Manipulation Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/throttler.html">Throttler</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/time.html">Times and Dates</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uploaded_files.html">Working with Uploaded Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">Working with URIs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/validation.html">Validation</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/response.html">Testing Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli.html">Running via the Command Line</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Custom CLI Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
||
|
||
|
||
<nav class="wy-nav-top" aria-label="top navigation">
|
||
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="../index.html">CodeIgniter</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" class="icon icon-home"></a> »</li>
|
||
|
||
<li><a href="index.html">Building Responses</a> »</li>
|
||
|
||
<li>API Response Trait</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="api-response-trait">
|
||
<h1>API Response Trait<a class="headerlink" href="#api-response-trait" title="Permalink to this headline">¶</a></h1>
|
||
<p>Much of modern PHP development requires building API’s, whether simply to provide data for a javascript-heavy
|
||
single page application, or as a standalone product. CodeIgniter provides an API Response trait that can be
|
||
used with any controller to make common response types simple, with no need to remember which HTTP status code
|
||
should be returned for which response types.</p>
|
||
<div class="contents local topic" id="contents">
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="#example-usage" id="id1">Example Usage</a></p></li>
|
||
<li><p><a class="reference internal" href="#handling-response-types" id="id2">Handling Response Types</a></p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="#class-reference" id="id3">Class Reference</a></p></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="example-usage">
|
||
<h2><a class="toc-backref" href="#id1">Example Usage</a><a class="headerlink" href="#example-usage" title="Permalink to this headline">¶</a></h2>
|
||
<p>The following example shows a common usage pattern within your controllers.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\API\ResponseTrait</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Users</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Controller</span>
|
||
<span class="p">{</span>
|
||
<span class="k">use</span> <span class="nx">ResponseTrait</span><span class="p">;</span>
|
||
|
||
<span class="k">public</span> <span class="k">function</span> <span class="nf">createUser</span><span class="p">()</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$model</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">UserModel</span><span class="p">();</span>
|
||
<span class="nv">$user</span> <span class="o">=</span> <span class="nv">$model</span><span class="o">-></span><span class="na">save</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">request</span><span class="o">-></span><span class="na">getPost</span><span class="p">());</span>
|
||
|
||
<span class="c1">// Respond with 201 status code</span>
|
||
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">respondCreated</span><span class="p">();</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In this example, an HTTP status code of 201 is returned, with the generic status message, ‘Created’. Methods
|
||
exist for the most common use cases:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Generic response method</span>
|
||
<span class="nx">respond</span><span class="p">(</span><span class="nv">$data</span><span class="p">,</span> <span class="mi">200</span><span class="p">);</span>
|
||
<span class="c1">// Generic failure response</span>
|
||
<span class="nx">fail</span><span class="p">(</span><span class="nv">$errors</span><span class="p">,</span> <span class="mi">400</span><span class="p">);</span>
|
||
<span class="c1">// Item created response</span>
|
||
<span class="nx">respondCreated</span><span class="p">(</span><span class="nv">$data</span><span class="p">);</span>
|
||
<span class="c1">// Item successfully deleted</span>
|
||
<span class="nx">respondDeleted</span><span class="p">(</span><span class="nv">$data</span><span class="p">);</span>
|
||
<span class="c1">// Command executed by no response required</span>
|
||
<span class="nx">respondNoContent</span><span class="p">(</span><span class="nv">$message</span><span class="p">);</span>
|
||
<span class="c1">// Client isn't authorized</span>
|
||
<span class="nx">failUnauthorized</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
<span class="c1">// Forbidden action</span>
|
||
<span class="nx">failForbidden</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
<span class="c1">// Resource Not Found</span>
|
||
<span class="nx">failNotFound</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
<span class="c1">// Data did not validate</span>
|
||
<span class="nx">failValidationError</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
<span class="c1">// Resource already exists</span>
|
||
<span class="nx">failResourceExists</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
<span class="c1">// Resource previously deleted</span>
|
||
<span class="nx">failResourceGone</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
<span class="c1">// Client made too many requests</span>
|
||
<span class="nx">failTooManyRequests</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="handling-response-types">
|
||
<h2><a class="toc-backref" href="#id2">Handling Response Types</a><a class="headerlink" href="#handling-response-types" title="Permalink to this headline">¶</a></h2>
|
||
<p>When you pass your data in any of these methods, they will determine the data type to format the results as based on
|
||
the following criteria:</p>
|
||
<ul class="simple">
|
||
<li><p>If $data is a string, it will be treated as HTML to send back to the client.</p></li>
|
||
<li><dl class="simple">
|
||
<dt>If $data is an array, it will be formatted according to the controller’s <code class="docutils literal notranslate"><span class="pre">$this->format</span></code> value. If that is empty</dt><dd><p>it will try to negotiate the content type with what the client asked for, defaulting to JSON
|
||
if nothing else has been specified within ConfigAPI.php, the <code class="docutils literal notranslate"><span class="pre">$supportedResponseFormats</span></code> property.</p>
|
||
</dd>
|
||
</dl>
|
||
</li>
|
||
</ul>
|
||
<p>To define the formatter that is used, edit <strong>Config/Format.php</strong>. The <code class="docutils literal notranslate"><span class="pre">$supportedResponseFormats</span></code> contains a list of
|
||
mime types that your application can automatically format the response for. By default, the system knows how to
|
||
format both XML and JSON responses:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nv">$supportedResponseFormats</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'application/json'</span><span class="p">,</span>
|
||
<span class="s1">'application/xml'</span>
|
||
<span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This is the array that is used during <a class="reference internal" href="../incoming/content_negotiation.html"><span class="doc">Content Negotiation</span></a> to determine which
|
||
type of response to return. If no matches are found between what the client requested and what you support, the first
|
||
format in this array is what will be returned.</p>
|
||
<p>Next, you need to define the class that is used to format the array of data. This must be a fully qualified class
|
||
name, and the class must implement <strong>CodeIgniter\Format\FormatterInterface</strong>. Formatters come out of the box that
|
||
support both JSON and XML:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nv">$formatters</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'application/json'</span> <span class="o">=></span> <span class="nx">\CodeIgniter\Format\JSONFormatter</span><span class="o">::</span><span class="na">class</span><span class="p">,</span>
|
||
<span class="s1">'application/xml'</span> <span class="o">=></span> <span class="nx">\CodeIgniter\Format\XMLFormatter</span><span class="o">::</span><span class="na">class</span>
|
||
<span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>So, if your request asks for JSON formatted data in an <strong>Accept</strong> header, the data array you pass any of the
|
||
<code class="docutils literal notranslate"><span class="pre">respond*</span></code> or <code class="docutils literal notranslate"><span class="pre">fail*</span></code> methods will be formatted by the <strong>CodeIgniter\API\JSONFormatter</strong> class. The resulting
|
||
JSON data will be sent back to the client.</p>
|
||
<div class="section" id="class-reference">
|
||
<h3><a class="toc-backref" href="#id3">Class Reference</a><a class="headerlink" href="#class-reference" title="Permalink to this headline">¶</a></h3>
|
||
<dl class="method">
|
||
<dt id="setResponseFormat">
|
||
<code class="sig-name descname">setResponseFormat</code><span class="sig-paren">(</span><em class="sig-param">$format</em><span class="sig-paren">)</span><a class="headerlink" href="#setResponseFormat" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>:param string $format The type of response to return, either <code class="docutils literal notranslate"><span class="pre">json</span></code> or <code class="docutils literal notranslate"><span class="pre">xml</span></code></p>
|
||
<p>This defines the format to be used when formatting arrays in responses. If you provide a <code class="docutils literal notranslate"><span class="pre">null</span></code> value for
|
||
<code class="docutils literal notranslate"><span class="pre">$format</span></code>, it will be automatically determined through content negotiation.</p>
|
||
</dd></dl>
|
||
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">setResponseFormat</span><span class="p">(</span><span class="s1">'json'</span><span class="p">)</span><span class="o">-></span><span class="na">respond</span><span class="p">([</span><span class="s1">'error'</span> <span class="o">=></span> <span class="k">false</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<dl class="method">
|
||
<dt id="respond">
|
||
<code class="sig-name descname">respond</code><span class="sig-paren">(</span><em class="sig-param">$data</em><span class="optional">[</span>, <em class="sig-param">$statusCode=200</em><span class="optional">[</span>, <em class="sig-param">$message=''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#respond" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$data</strong> (<em>mixed</em>) – The data to return to the client. Either string or array.</p></li>
|
||
<li><p><strong>$statusCode</strong> (<em>int</em>) – The HTTP status code to return. Defaults to 200</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
</dl>
|
||
<p>This is the method used by all other methods in this trait to return a response to the client.</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">$data</span></code> element can be either a string or an array. By default, a string will be returned as HTML,
|
||
while an array will be run through json_encode and returned as JSON, unless <a class="reference internal" href="../incoming/content_negotiation.html"><span class="doc">Content Negotiation</span></a>
|
||
determines it should be returned in a different format.</p>
|
||
<p>If a <code class="docutils literal notranslate"><span class="pre">$message</span></code> string is passed, it will be used in place of the standard IANA reason codes for the
|
||
response status. Not every client will respect the custom codes, though, and will use the IANA standards
|
||
that match the status code.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Since it sets the status code and body on the active Response instance, this should always
|
||
be the final method in the script execution.</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="fail">
|
||
<code class="sig-name descname">fail</code><span class="sig-paren">(</span><em class="sig-param">$messages</em><span class="optional">[</span>, <em class="sig-param">int $status=400</em><span class="optional">[</span>, <em class="sig-param">string $code=null</em><span class="optional">[</span>, <em class="sig-param">string $message=''</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#fail" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$messages</strong> (<em>mixed</em>) – A string or array of strings that contain error messages encountered.</p></li>
|
||
<li><p><strong>$status</strong> (<em>int</em>) – The HTTP status code to return. Defaults to 400.</p></li>
|
||
<li><p><strong>$code</strong> (<em>string</em>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>A multi-part response in the client’s preferred format.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>The is the generic method used to represent a failed response, and is used by all of the other “fail” methods.</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">$messages</span></code> element can be either a string or an array of strings.</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">$status</span></code> parameter is the HTTP status code that should be returned.</p>
|
||
<p>Since many APIs are better served using custom error codes, a custom error code can be passed in the third
|
||
parameter. If no value is present, it will be the same as <code class="docutils literal notranslate"><span class="pre">$status</span></code>.</p>
|
||
<p>If a <code class="docutils literal notranslate"><span class="pre">$message</span></code> string is passed, it will be used in place of the standard IANA reason codes for the
|
||
response status. Not every client will respect the custom codes, though, and will use the IANA standards
|
||
that match the status code.</p>
|
||
<p>The response is an array with two elements: <code class="docutils literal notranslate"><span class="pre">error</span></code> and <code class="docutils literal notranslate"><span class="pre">messages</span></code>. The <code class="docutils literal notranslate"><span class="pre">error</span></code> element contains the status
|
||
code of the error. The <code class="docutils literal notranslate"><span class="pre">messages</span></code> element contains an array of error messages. It would look something like:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$response</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'status'</span> <span class="o">=></span> <span class="mi">400</span><span class="p">,</span>
|
||
<span class="s1">'code'</span> <span class="o">=></span> <span class="s1">'321a'</span><span class="p">,</span>
|
||
<span class="s1">'messages'</span> <span class="o">=></span> <span class="p">[</span>
|
||
<span class="s1">'Error message 1'</span><span class="p">,</span>
|
||
<span class="s1">'Error message 2'</span>
|
||
<span class="p">]</span>
|
||
<span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="respondCreated">
|
||
<code class="sig-name descname">respondCreated</code><span class="sig-paren">(</span><em class="sig-param">$data = null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#respondCreated" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$data</strong> (<em>mixed</em>) – The data to return to the client. Either string or array.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when a new resource was created, typically 201.:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$user</span> <span class="o">=</span> <span class="nv">$userModel</span><span class="o">-></span><span class="na">insert</span><span class="p">(</span><span class="nv">$data</span><span class="p">);</span>
|
||
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">respondCreated</span><span class="p">(</span><span class="nv">$user</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="respondDeleted">
|
||
<code class="sig-name descname">respondDeleted</code><span class="sig-paren">(</span><em class="sig-param">$data = null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#respondDeleted" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$data</strong> (<em>mixed</em>) – The data to return to the client. Either string or array.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when a new resource was deleted as the result of this API call, typically 200.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$user</span> <span class="o">=</span> <span class="nv">$userModel</span><span class="o">-></span><span class="na">delete</span><span class="p">(</span><span class="nv">$id</span><span class="p">);</span>
|
||
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">respondDeleted</span><span class="p">([</span><span class="s1">'id'</span> <span class="o">=></span> <span class="nv">$id</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="respondNoContent">
|
||
<code class="sig-name descname">respondNoContent</code><span class="sig-paren">(</span><em class="sig-param">string $message = 'No Content'</em><span class="sig-paren">)</span><a class="headerlink" href="#respondNoContent" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when a command was successfully executed by the server but there is no
|
||
meaningful reply to send back to the client, typically 204.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nb">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
|
||
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">respondNoContent</span><span class="p">();</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="failUnauthorized">
|
||
<code class="sig-name descname">failUnauthorized</code><span class="sig-paren">(</span><em class="sig-param">string $description = 'Unauthorized'</em><span class="optional">[</span>, <em class="sig-param">string $code=null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#failUnauthorized" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$description</strong> (<em>string</em>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<em>string</em>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when the user either has not been authorized,
|
||
or has incorrect authorization. Status code is 401.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">failUnauthorized</span><span class="p">(</span><span class="s1">'Invalid Auth token'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="failForbidden">
|
||
<code class="sig-name descname">failForbidden</code><span class="sig-paren">(</span><em class="sig-param">string $description = 'Forbidden'</em><span class="optional">[</span>, <em class="sig-param">string $code=null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#failForbidden" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$description</strong> (<em>string</em>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<em>string</em>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Unlike <code class="docutils literal notranslate"><span class="pre">failUnauthorized</span></code>, this method should be used when the requested API endpoint is never allowed.
|
||
Unauthorized implies the client is encouraged to try again with different credentials. Forbidden means
|
||
the client should not try again because it won’t help. Status code is 403.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">failForbidden</span><span class="p">(</span><span class="s1">'Invalid API endpoint.'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="failNotFound">
|
||
<code class="sig-name descname">failNotFound</code><span class="sig-paren">(</span><em class="sig-param">string $description = 'Not Found'</em><span class="optional">[</span>, <em class="sig-param">string $code=null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#failNotFound" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$description</strong> (<em>string</em>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<em>string</em>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when the requested resource cannot be found. Status code is 404.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">failNotFound</span><span class="p">(</span><span class="s1">'User 13 cannot be found.'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="failValidationErrors">
|
||
<code class="sig-name descname">failValidationErrors</code><span class="sig-paren">(</span><em class="sig-param">$errors</em><span class="optional">[</span>, <em class="sig-param">string $code=null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#failValidationErrors" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$errors</strong> (<em>mixed</em>) – The error message or array of messages to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<em>string</em>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when data the client sent did not pass validation rules. Status code is typically 400.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">failValidationErrors</span><span class="p">(</span><span class="nv">$validation</span><span class="o">-></span><span class="na">getErrors</span><span class="p">());</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="failResourceExists">
|
||
<code class="sig-name descname">failResourceExists</code><span class="sig-paren">(</span><em class="sig-param">string $description = 'Conflict'</em><span class="optional">[</span>, <em class="sig-param">string $code=null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#failResourceExists" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$description</strong> (<em>string</em>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<em>string</em>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when the resource the client is trying to create already exists.
|
||
Status code is typically 409.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">failResourceExists</span><span class="p">(</span><span class="s1">'A user already exists with that email.'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="failResourceGone">
|
||
<code class="sig-name descname">failResourceGone</code><span class="sig-paren">(</span><em class="sig-param">string $description = 'Gone'</em><span class="optional">[</span>, <em class="sig-param">string $code=null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#failResourceGone" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$description</strong> (<em>string</em>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<em>string</em>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when the requested resource was previously deleted and
|
||
is no longer available. Status code is typically 410.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">failResourceGone</span><span class="p">(</span><span class="s1">'That user has been previously deleted.'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="failTooManyRequests">
|
||
<code class="sig-name descname">failTooManyRequests</code><span class="sig-paren">(</span><em class="sig-param">string $description = 'Too Many Requests'</em><span class="optional">[</span>, <em class="sig-param">string $code=null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#failTooManyRequests" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$description</strong> (<em>string</em>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<em>string</em>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when the client has called an API endpoint too many times.
|
||
This might be due to some form of throttling or rate limiting. Status code is typically 400.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">failTooManyRequests</span><span class="p">(</span><span class="s1">'You must wait 15 seconds before making another request.'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="failServerError">
|
||
<code class="sig-name descname">failServerError</code><span class="sig-paren">(</span><em class="sig-param">string $description = 'Internal Server Error'</em><span class="optional">[</span>, <em class="sig-param">string $code = null</em><span class="optional">[</span>, <em class="sig-param">string $message = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#failServerError" title="Permalink to this definition">¶</a></dt>
|
||
<dd><dl class="field-list simple">
|
||
<dt class="field-odd">Parameters</dt>
|
||
<dd class="field-odd"><ul class="simple">
|
||
<li><p><strong>$description</strong> (<em>string</em>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<em>string</em>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<em>string</em>) – A custom “reason” message to return.</p></li>
|
||
</ul>
|
||
</dd>
|
||
<dt class="field-even">Returns</dt>
|
||
<dd class="field-even"><p>The value of the Response object’s send() method.</p>
|
||
</dd>
|
||
</dl>
|
||
<p>Sets the appropriate status code to use when there is a server error.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">failServerError</span><span class="p">(</span><span class="s1">'Server error.'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
<footer>
|
||
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
||
<a href="localization.html" class="btn btn-neutral float-right" title="Localization" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
||
<a href="response.html" class="btn btn-neutral float-left" title="HTTP Responses" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
|
||
</div>
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>
|
||
© Copyright 2019-2021 CodeIgniter Foundation.
|
||
<span class="lastupdated">
|
||
Last updated on May 25, 2021.
|
||
</span>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
|
||
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
|
||
|
||
<a href="https://github.com/readthedocs/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">
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(false);
|
||
});
|
||
</script>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</body>
|
||
</html> |