CodeIgniter4/general/configuration.html
2022-04-09 00:27:21 +00:00

658 lines
50 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>Configuration &mdash; CodeIgniter 4.1.9 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 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 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="CodeIgniter URLs" href="urls.html" />
<link rel="prev" title="General Topics" href="index.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="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="../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/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 class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">General Topics</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="#">Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="urls.html">CodeIgniter URLs</a></li>
<li class="toctree-l2"><a class="reference internal" href="helpers.html">Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="common_functions.html">Global Functions and Constants</a></li>
<li class="toctree-l2"><a class="reference internal" href="logging.html">Logging Information</a></li>
<li class="toctree-l2"><a class="reference internal" href="errors.html">Error Handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="caching.html">Web Page Caching</a></li>
<li class="toctree-l2"><a class="reference internal" href="ajax.html">AJAX Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="modules.html">Code Modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="managing_apps.html">Managing your Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="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>
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/views.html">Views</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_cells.html">View Cells</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_renderer.html">View Renderer</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_layouts.html">View Layouts</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_parser.html">View Parser</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_decorators.html">View Decorators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/table.html">HTML Table Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/response.html">HTTP Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/api_responses.html">API Response Trait</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/localization.html">Localization</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/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/files.html#file-collections">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.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="Mobile navigation menu" style="background: #DD4814" >
<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="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li><a href="index.html">General Topics</a> &raquo;</li>
<li>Configuration</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="configuration">
<h1>Configuration<a class="headerlink" href="#configuration" title="Permalink to this headline"></a></h1>
<p>Every framework uses configuration files to define numerous parameters and
initial settings. CodeIgniter configuration files define simple classes where
the required settings are public properties.</p>
<p>Unlike many other frameworks, CodeIgniter configurable items arent contained in
a single file. Instead, each class that needs configurable items will have a
configuration file with the same name as the class that uses it. You will find
the application configuration files in the <strong>/app/Config</strong> folder.</p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#working-with-configuration-files" id="id2">Working With Configuration Files</a></p></li>
<li><p><a class="reference internal" href="#creating-configuration-files" id="id3">Creating Configuration Files</a></p></li>
<li><p><a class="reference internal" href="#environment-variables" id="id4">Environment Variables</a></p></li>
<li><p><a class="reference internal" href="#environment-variables-and-codeigniter" id="id5">Environment Variables and CodeIgniter</a></p></li>
<li><p><a class="reference internal" href="#nesting-variables" id="id6">Nesting Variables</a></p></li>
<li><p><a class="reference internal" href="#namespaced-variables" id="id7">Namespaced Variables</a></p></li>
<li><p><a class="reference internal" href="#configuration-classes-and-environment-variables" id="id8">Configuration Classes and Environment Variables</a></p></li>
<li><p><a class="reference internal" href="#environment-variables-as-replacements-for-data" id="id9">Environment Variables as Replacements for Data</a></p></li>
<li><p><a class="reference internal" href="#treating-environment-variables-as-arrays" id="id10">Treating Environment Variables as Arrays</a></p></li>
<li><p><a class="reference internal" href="#handling-different-environments" id="id11">Handling Different Environments</a></p></li>
<li><p><a class="reference internal" href="#registrars" id="id12">Registrars</a></p>
<ul>
<li><p><a class="reference internal" href="#implicit-registrars" id="id13">Implicit Registrars</a></p></li>
<li><p><a class="reference internal" href="#explicit-registrars" id="id14">Explicit Registrars</a></p></li>
</ul>
</li>
</ul>
</div>
<section id="working-with-configuration-files">
<h2><a class="toc-backref" href="#id2">Working With Configuration Files</a><a class="headerlink" href="#working-with-configuration-files" title="Permalink to this headline"></a></h2>
<p>You can access configuration files for your classes in several different ways.</p>
<ul>
<li><p>By using the <code class="docutils literal notranslate"><span class="pre">new</span></code> keyword to create an instance:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Creating new configuration object by hand</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Config\Pager</span><span class="p">();</span>
</pre></div>
</div>
</li>
<li><p>By using the <code class="docutils literal notranslate"><span class="pre">config()</span></code> function:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Get shared instance with config function</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="nx">config</span><span class="p">(</span><span class="s1">&#39;Pager&#39;</span><span class="p">);</span>
<span class="c1">// Access config class with namespace</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="nx">config</span><span class="p">(</span><span class="s1">&#39;Config\\Pager&#39;</span><span class="p">);</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="nx">config</span><span class="p">(</span><span class="nx">\Config\Pager</span><span class="o">::</span><span class="na">class</span><span class="p">);</span>
<span class="c1">// Creating a new object with config function</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="nx">config</span><span class="p">(</span><span class="s1">&#39;Pager&#39;</span><span class="p">,</span> <span class="k">false</span><span class="p">);</span>
</pre></div>
</div>
</li>
</ul>
<p>All configuration object properties are public, so you access the settings like any other property:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="nx">config</span><span class="p">(</span><span class="s1">&#39;Pager&#39;</span><span class="p">);</span>
<span class="c1">// Access settings as object properties</span>
<span class="nv">$pageSize</span> <span class="o">=</span> <span class="nv">$config</span><span class="o">-&gt;</span><span class="na">perPage</span><span class="p">;</span>
</pre></div>
</div>
<p>If no namespace is provided, it will look for the file in all defined namespaces
as well as <strong>/app/Config/</strong>.</p>
<p>All of the configuration files that ship with CodeIgniter are namespaced with
<code class="docutils literal notranslate"><span class="pre">Config</span></code>. Using this namespace in your application will provide the best
performance since it knows exactly where to find the files.</p>
<p>You can put configuration files in any folder you want by using a different namespace.
This allows you to put configuration files on the production server in a folder
that is not web-accessible while keeping it under <strong>/app</strong> for easy access
during development.</p>
</section>
<section id="creating-configuration-files">
<h2><a class="toc-backref" href="#id3">Creating Configuration Files</a><a class="headerlink" href="#creating-configuration-files" title="Permalink to this headline"></a></h2>
<p>When you need a new configuration, first you create a new file at your desired location.
The default file location (recommended for most cases) is <strong>/app/Config</strong>.
The class should use the appropriate namespace, and it should extend
<code class="docutils literal notranslate"><span class="pre">CodeIgniter\Config\BaseConfig</span></code> to ensure that it can receive environment-specific settings.</p>
<p>Define the class and fill it with public properties that represent your settings:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</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">CustomClass</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
<span class="p">{</span>
<span class="k">public</span> <span class="nv">$siteName</span> <span class="o">=</span> <span class="s1">&#39;My Great Site&#39;</span><span class="p">;</span>
<span class="k">public</span> <span class="nv">$siteEmail</span> <span class="o">=</span> <span class="s1">&#39;webmaster@example.com&#39;</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="environment-variables">
<h2><a class="toc-backref" href="#id4">Environment Variables</a><a class="headerlink" href="#environment-variables" title="Permalink to this headline"></a></h2>
<p>One of todays best practices for application setup is to use Environment Variables. One reason for this is that Environment Variables are easy to change between deploys without changing any code. Configuration can change a lot across deploys, but code does not. For instance, multiple environments, such as the developers local machine and the production server, usually need different configuration values for each particular setup.</p>
<p>Environment Variables should also be used for anything private such as passwords, API keys, or other sensitive data.</p>
</section>
<section id="environment-variables-and-codeigniter">
<h2><a class="toc-backref" href="#id5">Environment Variables and CodeIgniter</a><a class="headerlink" href="#environment-variables-and-codeigniter" title="Permalink to this headline"></a></h2>
<p>CodeIgniter makes it simple and painless to set Environment Variables by using a “dotenv” file. The term comes from the file name, which starts with a dot before the text “env”.</p>
<p>CodeIgniter expects <strong>.env</strong> to be at the root of your project alongside the <code class="docutils literal notranslate"><span class="pre">system</span></code>
and <code class="docutils literal notranslate"><span class="pre">app</span></code> directories. There is a template file distributed with CodeIgniter thats
located at the project root named <strong>env</strong> (Notice theres no dot (<strong>.</strong>) at the start?).
It has a large collection of variables your project might use that have been assigned
empty, dummy, or default values. You can use this file as a starting place for your
application by either renaming the template to <strong>.env</strong>, or by making a copy of it named <strong>.env</strong>.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>Make sure the <strong>.env</strong> file is NOT tracked by your version control system. For <em>git</em> that means adding it to <strong>.gitignore</strong>. Failure to do so could result in sensitive credentials being exposed to the public.</p>
</div>
<p>Settings are stored in <strong>.env</strong> files as a simple a collection of name/value pairs separated by an equal sign.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">S3_BUCKET</span> <span class="o">=</span> <span class="nx">dotenv</span>
<span class="nx">SECRET_KEY</span> <span class="o">=</span> <span class="nx">super_secret_key</span>
<span class="nx">CI_ENVIRONMENT</span> <span class="o">=</span> <span class="nx">development</span>
</pre></div>
</div>
<p>When your application runs, <strong>.env</strong> will be loaded automatically, and the variables put
into the environment. If a variable already exists in the environment, it will NOT be
overwritten. The loaded Environment variables are accessed using any of the following:
<code class="docutils literal notranslate"><span class="pre">getenv()</span></code>, <code class="docutils literal notranslate"><span class="pre">$_SERVER</span></code>, or <code class="docutils literal notranslate"><span class="pre">$_ENV</span></code>.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$s3_bucket</span> <span class="o">=</span> <span class="nb">getenv</span><span class="p">(</span><span class="s1">&#39;S3_BUCKET&#39;</span><span class="p">);</span>
<span class="nv">$s3_bucket</span> <span class="o">=</span> <span class="nv">$_ENV</span><span class="p">[</span><span class="s1">&#39;S3_BUCKET&#39;</span><span class="p">];</span>
<span class="nv">$s3_bucket</span> <span class="o">=</span> <span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;S3_BUCKET&#39;</span><span class="p">];</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Note that your settings from the <strong>.env</strong> file are added to Environment Variables. As a side effect, this means that if your CodeIgniter application is (for example) generating a <code class="docutils literal notranslate"><span class="pre">var_dump($_ENV)</span></code> or <code class="docutils literal notranslate"><span class="pre">phpinfo()</span></code> (for debugging or other valid reasons) <strong>your secure credentials are publicly exposed</strong>.</p>
</div>
</section>
<section id="nesting-variables">
<h2><a class="toc-backref" href="#id6">Nesting Variables</a><a class="headerlink" href="#nesting-variables" title="Permalink to this headline"></a></h2>
<p>To save on typing, you can reuse variables that youve already specified in the file by wrapping the
variable name within <code class="docutils literal notranslate"><span class="pre">${...}</span></code>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">BASE_DIR</span><span class="o">=</span><span class="s2">&quot;/var/webroot/project-root&quot;</span>
<span class="nx">CACHE_DIR</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">BASE_DIR</span><span class="si">}</span><span class="s2">/cache&quot;</span>
<span class="nx">TMP_DIR</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">BASE_DIR</span><span class="si">}</span><span class="s2">/tmp&quot;</span>
</pre></div>
</div>
</section>
<section id="namespaced-variables">
<h2><a class="toc-backref" href="#id7">Namespaced Variables</a><a class="headerlink" href="#namespaced-variables" title="Permalink to this headline"></a></h2>
<p>There will be times when you will have several variables with the same name.
The system needs a way of knowing what the correct setting should be.
This problem is solved by “<em>namespacing</em>” the variables.</p>
<p>Namespaced variables use a dot notation to qualify variable names so they will be unique
when incorporated into the environment. This is done by including a distinguishing
prefix followed by a dot (.), and then the variable name itself.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// not namespaced variables</span>
<span class="nx">name</span> <span class="o">=</span> <span class="s2">&quot;George&quot;</span>
<span class="nx">db</span><span class="o">=</span><span class="nx">my_db</span>
<span class="c1">// namespaced variables</span>
<span class="nx">address</span><span class="o">.</span><span class="nx">city</span> <span class="o">=</span> <span class="s2">&quot;Berlin&quot;</span>
<span class="nx">address</span><span class="o">.</span><span class="nx">country</span> <span class="o">=</span> <span class="s2">&quot;Germany&quot;</span>
<span class="nx">frontend</span><span class="o">.</span><span class="nx">db</span> <span class="o">=</span> <span class="nx">sales</span>
<span class="nx">backend</span><span class="o">.</span><span class="nx">db</span> <span class="o">=</span> <span class="nx">admin</span>
<span class="nx">BackEnd</span><span class="o">.</span><span class="nx">db</span> <span class="o">=</span> <span class="nx">admin</span>
</pre></div>
</div>
</section>
<section id="configuration-classes-and-environment-variables">
<h2><a class="toc-backref" href="#id8">Configuration Classes and Environment Variables</a><a class="headerlink" href="#configuration-classes-and-environment-variables" title="Permalink to this headline"></a></h2>
<p>When you instantiate a configuration class, any <em>namespaced</em> environment variables
are considered for merging into the configuration objects properties.</p>
<p>If the prefix of a namespaced variable exactly matches the namespace of the configuration
class, then the trailing part of the setting (after the dot) is treated as a configuration
property. If it matches an existing configuration property, the environment variables
value will replace the corresponding value from the configuration file. If there is no match,
the configuration class properties are left unchanged. In this usage, the prefix must be
the full (case-sensitive) namespace of the class.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">Config\App</span><span class="o">.</span><span class="nx">forceGlobalSecureRequests</span> <span class="o">=</span> <span class="k">true</span>
<span class="nx">Config\App</span><span class="o">.</span><span class="nx">CSPEnabled</span> <span class="o">=</span> <span class="k">true</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Both the namespace prefix and the property name are case-sensitive. They must exactly match the full namespace and property names as defined in the configuration class file.</p>
</div>
<p>The same holds for a <em>short prefix</em>, which is a namespace using only the lowercase version of
the configuration class name. If the short prefix matches the class name,
the value from <strong>.env</strong> replaces the configuration file value.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">app</span><span class="o">.</span><span class="nx">forceGlobalSecureRequests</span> <span class="o">=</span> <span class="k">true</span>
<span class="nx">app</span><span class="o">.</span><span class="nx">CSPEnabled</span> <span class="o">=</span> <span class="k">true</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>When using the <em>short prefix</em> the property names must still exactly match the class defined name.</p>
</div>
<p>Some environments do not permit variable name with dots. In such case, you could also use <code class="docutils literal notranslate"><span class="pre">_</span></code> as a seperator.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">app_forceGlobalSecureRequests</span> <span class="o">=</span> <span class="k">true</span>
<span class="nx">app_CSPEnabled</span> <span class="o">=</span> <span class="k">true</span>
</pre></div>
</div>
</section>
<section id="environment-variables-as-replacements-for-data">
<h2><a class="toc-backref" href="#id9">Environment Variables as Replacements for Data</a><a class="headerlink" href="#environment-variables-as-replacements-for-data" title="Permalink to this headline"></a></h2>
<p>It is very important to always remember that environment variables contained in your <strong>.env</strong> are
<strong>only replacements for existing data</strong>. This means that you cannot expect to fill your <strong>.env</strong> with all
the replacements for your configurations but have nothing to receive these replacements in the
related configuration file(s).</p>
<p>The <strong>.env</strong> only serves to fill or replace the values in your configuration files. That said, your
configuration files should have a container or receiving property for those. Adding so many variables in
your <strong>.env</strong> with nothing to contain them in the receiving end is useless.</p>
<p>Simply put, you cannot just put <code class="docutils literal notranslate"><span class="pre">app.myNewConfig</span> <span class="pre">=</span> <span class="pre">foo</span></code> in your <strong>.env</strong> and expect your <code class="docutils literal notranslate"><span class="pre">Config\App</span></code>
to magically have that property and value at run time.</p>
</section>
<section id="treating-environment-variables-as-arrays">
<h2><a class="toc-backref" href="#id10">Treating Environment Variables as Arrays</a><a class="headerlink" href="#treating-environment-variables-as-arrays" title="Permalink to this headline"></a></h2>
<p>A namespaced environment variable can be further treated as an array.
If the prefix matches the configuration class, then the remainder of the
environment variable name is treated as an array reference if it also
contains a dot.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// regular namespaced variable</span>
<span class="nx">Config\SimpleConfig</span><span class="o">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">George</span>
<span class="c1">// array namespaced variables</span>
<span class="nx">Config\SimpleConfig</span><span class="o">.</span><span class="nx">address</span><span class="o">.</span><span class="nx">city</span> <span class="o">=</span> <span class="s2">&quot;Berlin&quot;</span>
<span class="nx">Config\SimpleConfig</span><span class="o">.</span><span class="nx">address</span><span class="o">.</span><span class="nx">country</span> <span class="o">=</span> <span class="s2">&quot;Germany&quot;</span>
</pre></div>
</div>
<p>If this was referring to a SimpleConfig configuration object, the above example would be treated as:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$address</span><span class="p">[</span><span class="s1">&#39;city&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Berlin&#39;</span><span class="p">;</span>
<span class="nv">$address</span><span class="p">[</span><span class="s1">&#39;country&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Germany&#39;</span><span class="p">;</span>
</pre></div>
</div>
<p>Any other elements of the <code class="docutils literal notranslate"><span class="pre">$address</span></code> property would be unchanged.</p>
<p>You can also use the array property name as a prefix. If the environment file
held the following then the result would be the same as above.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// array namespaced variables</span>
<span class="nx">Config\SimpleConfig</span><span class="o">.</span><span class="nx">address</span><span class="o">.</span><span class="nx">city</span> <span class="o">=</span> <span class="s2">&quot;Berlin&quot;</span>
<span class="nx">address</span><span class="o">.</span><span class="nx">country</span> <span class="o">=</span> <span class="s2">&quot;Germany&quot;</span>
</pre></div>
</div>
</section>
<section id="handling-different-environments">
<h2><a class="toc-backref" href="#id11">Handling Different Environments</a><a class="headerlink" href="#handling-different-environments" title="Permalink to this headline"></a></h2>
<p>Configuring multiple environments is easily accomplished by using a separate <strong>.env</strong> file with values modified to meet that environments needs.</p>
<p>The file should not contain every possible setting for every configuration class used by the application. In truth, it should include only those items that are specific to the environment or are sensitive details like passwords and API keys and other information that should not be exposed. But anything that changes between deployments is fair-game.</p>
<p>In each environment, place the <strong>.env</strong> file in the projects root folder. For most setups, this will be the same level as the <code class="docutils literal notranslate"><span class="pre">system</span></code> and <code class="docutils literal notranslate"><span class="pre">app</span></code> directories.</p>
<p>Do not track <strong>.env</strong> files with your version control system. If you do, and the repository is made public, you will have put sensitive information where everybody can find it.</p>
</section>
<section id="registrars">
<span id="id1"></span><h2><a class="toc-backref" href="#id12">Registrars</a><a class="headerlink" href="#registrars" title="Permalink to this headline"></a></h2>
<p>“Registrars” are any other classes which might provide additional configuration properties.
Registrars provide a means of altering a configuration at runtime across namespaces and files.
There are two ways to implement a Registrar: implicit and explicit.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Values from <strong>.env</strong> always take priority over Registrars.</p>
</div>
<section id="implicit-registrars">
<h3><a class="toc-backref" href="#id13">Implicit Registrars</a><a class="headerlink" href="#implicit-registrars" title="Permalink to this headline"></a></h3>
<p>Any namespace may define registrars by using the <strong>Config/Registrar.php</strong> file, if discovery
is enabled in <a class="reference internal" href="modules.html"><span class="doc">Modules</span></a>. These files are classes whose methods are
named for each configuration class you wish to extend. For example, a third-party module might
wish to supply an additional template to <code class="docutils literal notranslate"><span class="pre">Pager</span></code> without overwriting whatever a develop has
already configured. In <strong>src/Config/Registrar.php</strong> there would be a <code class="docutils literal notranslate"><span class="pre">Registrar</span></code> class with
the single <code class="docutils literal notranslate"><span class="pre">Pager()</span></code> method (note the case-sensitivity):</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">class</span> <span class="nc">Registrar</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">static</span> <span class="k">function</span> <span class="nf">Pager</span><span class="p">()</span><span class="o">:</span> <span class="k">array</span>
<span class="p">{</span>
<span class="k">return</span> <span class="p">[</span>
<span class="s1">&#39;templates&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
<span class="s1">&#39;module_pager&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;MyModule\Views\Pager&#39;</span><span class="p">,</span>
<span class="p">],</span>
<span class="p">];</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Registrar methods must always return an array, with keys corresponding to the properties
of the target config file. Existing values are merged, and Registrar properties have
overwrite priority.</p>
</section>
<section id="explicit-registrars">
<h3><a class="toc-backref" href="#id14">Explicit Registrars</a><a class="headerlink" href="#explicit-registrars" title="Permalink to this headline"></a></h3>
<p>A configuration file can also specify any number of registrars explicitly.
This is done by adding a <code class="docutils literal notranslate"><span class="pre">$registrars</span></code> property to your configuration file,
holding an array of the names of candidate registrars:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">MyConfig</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">static</span> <span class="nv">$registrars</span> <span class="o">=</span> <span class="p">[</span>
<span class="nx">SupportingPackageRegistrar</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>In order to act as a “registrar” the classes so identified must have a
static function with the same name as the configuration class, and it should return an associative
array of property settings.</p>
<p>When your configuration object is instantiated, it will loop over the
designated classes in <code class="docutils literal notranslate"><span class="pre">$registrars</span></code>. For each of these classes it will invoke
the method named for the configuration class and incorporate any returned properties.</p>
<p>A sample configuration class setup for this:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">App\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">MySalesConfig</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
<span class="p">{</span>
<span class="k">public</span> <span class="nv">$target</span> <span class="o">=</span> <span class="mi">100</span><span class="p">;</span>
<span class="k">public</span> <span class="nv">$campaign</span> <span class="o">=</span> <span class="s1">&#39;Winter Wonderland&#39;</span><span class="p">;</span>
<span class="k">public</span> <span class="k">static</span> <span class="nv">$registrars</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;\App\Models\RegionalSales&#39;</span><span class="p">,</span>
<span class="p">];</span>
<span class="p">}</span>
</pre></div>
</div>
<p>… and the associated regional sales model might look like:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">App\Models</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">RegionalSales</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">static</span> <span class="k">function</span> <span class="nf">MySalesConfig</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="p">[</span>
<span class="s1">&#39;target&#39;</span> <span class="o">=&gt;</span> <span class="mi">45</span><span class="p">,</span>
<span class="s1">&#39;actual&#39;</span> <span class="o">=&gt;</span> <span class="mi">72</span><span class="p">,</span>
<span class="p">];</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>With the above example, when <code class="docutils literal notranslate"><span class="pre">MySalesConfig</span></code> is instantiated, it will end up with
the two properties declared, but the value of the <code class="docutils literal notranslate"><span class="pre">$target</span></code> property will be overridden
by treating <code class="docutils literal notranslate"><span class="pre">RegionalSales</span></code> as a “registrar”. The resulting configuration properties:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$target</span> <span class="o">=</span> <span class="mi">45</span><span class="p">;</span>
<span class="nv">$campaign</span> <span class="o">=</span> <span class="s1">&#39;Winter Wonderland&#39;</span><span class="p">;</span>
</pre></div>
</div>
</section>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="index.html" class="btn btn-neutral float-left" title="General Topics" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="urls.html" class="btn btn-neutral float-right" title="CodeIgniter URLs" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>&#169; Copyright 2019-2022 CodeIgniter Foundation.
<span class="lastupdated">Last updated on Apr 09, 2022.
</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>