mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
805 lines
63 KiB
HTML
805 lines
63 KiB
HTML
<!DOCTYPE html>
|
||
<html class="writer-html5" lang="en" >
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>API Response Trait — CodeIgniter 4.4.4 documentation</title>
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/css/citheme_dark.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 data-url_root="../" id="documentation_options" 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/js/citheme.js"></script>
|
||
<script src="../_static/js/carbon.js"></script>
|
||
<script 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="Content Security Policy" href="csp.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" >
|
||
|
||
|
||
|
||
<a href="../index.html">
|
||
|
||
<img src="../_static/ci-logo-text.svg" 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" aria-label="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="Navigation menu">
|
||
<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>
|
||
<li class="toctree-l2"><a class="reference internal" href="../license.html">License Agreement</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/troubleshooting.html">Troubleshooting</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../changelogs/index.html">Change Logs</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/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/routing.html">URI Routing</a></li>
|
||
<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/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="view_decorators.html">View Decorators</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="csp.html">Content Security Policy</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>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/db_commands.html">Database Commands</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/file_collections.html">File Collections</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/publisher.html">Publisher</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>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/mocking.html">Mocking</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_overview.html">CLI Overview</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_controllers.html">Running Controllers via CLI</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/spark_commands.html">Spark Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Creating Spark 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/composer_packages.html">Creating Composer Packages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../libraries/official_packages.html">Official Packages</a></li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
|
||
<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">
|
||
<a class="btn btn-neutral float-right" href="https://github.com/codeigniter4/CodeIgniter4/edit/develop/user_guide_src/source/outgoing/api_responses.rst">Edit this page</a>
|
||
|
||
<div role="navigation" aria-label="Page navigation">
|
||
<ul class="wy-breadcrumbs">
|
||
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
|
||
<li class="breadcrumb-item"><a href="index.html">Building Responses</a></li>
|
||
<li class="breadcrumb-item active">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">
|
||
|
||
<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 APIs, 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></li>
|
||
<li><p><a class="reference internal" href="#class-reference" id="id3">Class Reference</a></p></li>
|
||
</ul>
|
||
</div>
|
||
<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">use</span> <span class="nx">CodeIgniter\Controller</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Users</span> <span class="k">extends</span> <span class="nx">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="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="c1">// Generic response method</span>
|
||
<span class="nv">$this</span><span class="o">-></span><span class="na">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="nv">$this</span><span class="o">-></span><span class="na">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="nv">$this</span><span class="o">-></span><span class="na">respondCreated</span><span class="p">(</span><span class="nv">$data</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Item successfully deleted</span>
|
||
<span class="nv">$this</span><span class="o">-></span><span class="na">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="nv">$this</span><span class="o">-></span><span class="na">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="nv">$this</span><span class="o">-></span><span class="na">failUnauthorized</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Forbidden action</span>
|
||
<span class="nv">$this</span><span class="o">-></span><span class="na">failForbidden</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Resource Not Found</span>
|
||
<span class="nv">$this</span><span class="o">-></span><span class="na">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="nv">$this</span><span class="o">-></span><span class="na">failValidationError</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Resource already exists</span>
|
||
<span class="nv">$this</span><span class="o">-></span><span class="na">failResourceExists</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Resource previously deleted</span>
|
||
<span class="nv">$this</span><span class="o">-></span><span class="na">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="nv">$this</span><span class="o">-></span><span class="na">failTooManyRequests</span><span class="p">(</span><span class="nv">$description</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<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><p>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,
|
||
it will try to negotiate the content type with what the client asked for, defaulting to JSON
|
||
if nothing else has been specified within <strong>Config/Format.php</strong>, the <code class="docutils literal notranslate"><span class="pre">$supportedResponseFormats</span></code> property.</p></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="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Format</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
|
||
<span class="p">{</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>
|
||
<span class="p">];</span>
|
||
|
||
<span class="c1">// ...</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 <code class="docutils literal notranslate"><span class="pre">CodeIgniter\Format\FormatterInterface</span></code>. 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="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
|
||
|
||
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Format</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
|
||
<span class="p">{</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>
|
||
<span class="p">];</span>
|
||
|
||
<span class="c1">// ...</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 <code class="docutils literal notranslate"><span class="pre">CodeIgniter\Format\JSONFormatter</span></code> class. The resulting
|
||
JSON data will be sent back to the client.</p>
|
||
</section>
|
||
<section id="class-reference">
|
||
<h2><a class="toc-backref" href="#id3">Class Reference</a><a class="headerlink" href="#class-reference" title="Permalink to this headline"></a></h2>
|
||
<dl class="php method">
|
||
<dt class="sig sig-object php" id="setResponseFormat">
|
||
<span class="sig-name descname"><span class="pre">setResponseFormat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">$format</span></em><span class="sig-paren">)</span><a class="headerlink" href="#setResponseFormat" 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>$format</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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></li>
|
||
</ul>
|
||
</dd>
|
||
</dl>
|
||
<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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="respond">
|
||
<span class="sig-name descname"><span class="pre">respond</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">$data</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">$statusCode</span> <span class="pre">=</span> <span class="pre">200</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">mixed</span></code></span>) – The data to return to the client. Either string or array.</p></li>
|
||
<li><p><strong>$statusCode</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">int</span></code></span>) – The HTTP status code to return. Defaults to 200</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="php method">
|
||
<dt class="sig sig-object php" id="fail">
|
||
<span class="sig-name descname"><span class="pre">fail</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">$messages</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">int</span> <span class="pre">$status</span> <span class="pre">=</span> <span class="pre">400</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$code</span> <span class="pre">=</span> <span class="pre">null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">mixed</span></code></span>) – A string or array of strings that contain error messages encountered.</p></li>
|
||
<li><p><strong>$status</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">int</span></code></span>) – The HTTP status code to return. Defaults to 400.</p></li>
|
||
<li><p><strong>$code</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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>
|
||
<span class="p">];</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="php method">
|
||
<dt class="sig sig-object php" id="respondCreated">
|
||
<span class="sig-name descname"><span class="pre">respondCreated</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">$data</span> <span class="pre">=</span> <span class="pre">null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">mixed</span></code></span>) – The data to return to the client. Either string or array.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="respondDeleted">
|
||
<span class="sig-name descname"><span class="pre">respondDeleted</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">$data</span> <span class="pre">=</span> <span class="pre">null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">mixed</span></code></span>) – The data to return to the client. Either string or array.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="respondNoContent">
|
||
<span class="sig-name descname"><span class="pre">respondNoContent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">'No</span> <span class="pre">Content'</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="failUnauthorized">
|
||
<span class="sig-name descname"><span class="pre">failUnauthorized</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">string</span> <span class="pre">$description</span> <span class="pre">=</span> <span class="pre">'Unauthorized'</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$code</span> <span class="pre">=</span> <span class="pre">null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="failForbidden">
|
||
<span class="sig-name descname"><span class="pre">failForbidden</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">string</span> <span class="pre">$description</span> <span class="pre">=</span> <span class="pre">'Forbidden'</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$code=null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="failNotFound">
|
||
<span class="sig-name descname"><span class="pre">failNotFound</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">string</span> <span class="pre">$description</span> <span class="pre">=</span> <span class="pre">'Not</span> <span class="pre">Found'</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$code=null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="failValidationErrors">
|
||
<span class="sig-name descname"><span class="pre">failValidationErrors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">$errors</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$code=null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">mixed</span></code></span>) – The error message or array of messages to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="failResourceExists">
|
||
<span class="sig-name descname"><span class="pre">failResourceExists</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">string</span> <span class="pre">$description</span> <span class="pre">=</span> <span class="pre">'Conflict'</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$code=null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="failResourceGone">
|
||
<span class="sig-name descname"><span class="pre">failResourceGone</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">string</span> <span class="pre">$description</span> <span class="pre">=</span> <span class="pre">'Gone'</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$code=null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="failTooManyRequests">
|
||
<span class="sig-name descname"><span class="pre">failTooManyRequests</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">string</span> <span class="pre">$description</span> <span class="pre">=</span> <span class="pre">'Too</span> <span class="pre">Many</span> <span class="pre">Requests'</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$code=null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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="php method">
|
||
<dt class="sig sig-object php" id="failServerError">
|
||
<span class="sig-name descname"><span class="pre">failServerError</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">string</span> <span class="pre">$description</span> <span class="pre">=</span> <span class="pre">'Internal</span> <span class="pre">Server</span> <span class="pre">Error'</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$code</span> <span class="pre">=</span> <span class="pre">null</span></em><span class="optional">[</span>, <em class="sig-param"><span class="pre">string</span> <span class="pre">$message</span> <span class="pre">=</span> <span class="pre">''</span></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> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – The error message to show the user.</p></li>
|
||
<li><p><strong>$code</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – A custom, API-specific, error code.</p></li>
|
||
<li><p><strong>$message</strong> (<span><code class="xref php php-obj docutils literal notranslate"><span class="pre">string</span></code></span>) – 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="o"><?</span><span class="nx">php</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>
|
||
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
|
||
<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>
|
||
<a href="csp.html" class="btn btn-neutral float-right" title="Content Security Policy" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
||
</div>
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>© Copyright 2019-2023 CodeIgniter Foundation.
|
||
<span class="lastupdated">Last updated on Dec 28, 2023.
|
||
</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>
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(false);
|
||
});
|
||
</script>
|
||
|
||
</body>
|
||
</html> |