CodeIgniter4/general/modules.html
2018-11-28 08:49:56 -08:00

476 lines
23 KiB
HTML

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Modules &mdash; CodeIgniter4 4.0.0-alpha.2 documentation</title>
<link rel="shortcut icon" href="../_static/ci-icon.ico"/>
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
<link rel="top" title="CodeIgniter4 4.0.0-alpha.2 documentation" href="../index.html"/>
<link rel="up" title="General Topics" href="index.html"/>
<link rel="next" title="Managing your Applications" href="managing_apps.html"/>
<link rel="prev" title="Web Page Caching" href="caching.html"/>
<script src="../_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../index.html" class="icon icon-home"> CodeIgniter4
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a></li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">General Topics</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="configuration.html">Working With Configuration Files</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 current"><a class="current reference internal" href="#">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></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../license.html">The MIT License (MIT)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">CodeIgniter4</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> &raquo;</li>
<li><a href="index.html">General Topics</a> &raquo;</li>
<li>Code Modules</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="code-modules">
<h1>Code Modules<a class="headerlink" href="#code-modules" title="Permalink to this headline"></a></h1>
<p>CodeIgniter supports a form of code modularization to help you create reusable code. Modules are typically
centered around a specific subject, and can be thought of as mini-applications within your larger application. Any
of the standard file types within the framework are supported, like controllers, models, views, config files, helpers,
language files, etc. Modules may contain as few, or as many, of these as you like.</p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#namespaces" id="id1">Namespaces</a></li>
<li><a class="reference internal" href="#auto-discovery" id="id2">Auto-Discovery</a><ul>
<li><a class="reference internal" href="#enable-disable-discover" id="id3">Enable/Disable Discover</a></li>
<li><a class="reference internal" href="#specify-discovery-items" id="id4">Specify Discovery Items</a></li>
</ul>
</li>
<li><a class="reference internal" href="#working-with-files" id="id5">Working With Files</a><ul>
<li><a class="reference internal" href="#routes" id="id6">Routes</a></li>
<li><a class="reference internal" href="#controllers" id="id7">Controllers</a></li>
<li><a class="reference internal" href="#config-files" id="id8">Config Files</a></li>
<li><a class="reference internal" href="#migrations" id="id9">Migrations</a></li>
<li><a class="reference internal" href="#seeds" id="id10">Seeds</a></li>
<li><a class="reference internal" href="#helpers" id="id11">Helpers</a></li>
<li><a class="reference internal" href="#language-files" id="id12">Language Files</a></li>
<li><a class="reference internal" href="#libraries" id="id13">Libraries</a></li>
<li><a class="reference internal" href="#models" id="id14">Models</a></li>
<li><a class="reference internal" href="#views" id="id15">Views</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="namespaces">
<h2><a class="toc-backref" href="#id1">Namespaces</a><a class="headerlink" href="#namespaces" title="Permalink to this headline"></a></h2>
<p>The core element of the modules functionality comes from the <a class="reference internal" href="../concepts/autoloader.html"><span class="doc">PSR4-compatible autoloading</span></a>
that CodeIgniter uses. While any code can use the PSR4 autoloader and namespaces, the only way to take full advantage of
modules is to namespace your code and add it to <strong>application/Config/Autoload.php</strong>, in the <code class="docutils literal"><span class="pre">psr4</span></code> section.</p>
<p>For example, let&#8217;s say we want to keep a simple blog module that we can re-use between applications. We might create
folder with our company name, Acme, to store all of our modules within. We will put it right alongside our <strong>application</strong>
directory in the main project root:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">/</span><span class="nx">acme</span> <span class="c1">// New modules directory</span>
<span class="o">/</span><span class="nx">application</span>
<span class="o">/</span><span class="k">public</span>
<span class="o">/</span><span class="nb">system</span>
<span class="o">/</span><span class="nx">tests</span>
<span class="o">/</span><span class="nx">writable</span>
</pre></div>
</div>
<p>Open <strong>application/Config/Autoload.php</strong> and add the <strong>Acme</strong> namespace to the <code class="docutils literal"><span class="pre">psr4</span></code> array property:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nv">$psr4</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;Acme&#39;</span> <span class="o">=&gt;</span> <span class="nx">ROOTPATH</span><span class="o">.</span><span class="s1">&#39;acme&#39;</span>
<span class="p">];</span>
</pre></div>
</div>
<p>Now that this is setup we can access any file within the <strong>acme</strong> folder through the <code class="docutils literal"><span class="pre">Acme</span></code> namespace. This alone
takes care of 80% of what is needed for modules to work, so you should be sure to familiarize yourself within namespaces
and become comfortable with their use. A number of the file types will be scanned for automatically through all defined
namespaces here, making this crucial to working with modules at all.</p>
<p>A common directory structure within a module will mimic the main application folder:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">/</span><span class="nx">acme</span>
<span class="o">/</span><span class="nx">Blog</span>
<span class="o">/</span><span class="nx">Config</span>
<span class="o">/</span><span class="nx">Controllers</span>
<span class="o">/</span><span class="nx">Database</span>
<span class="o">/</span><span class="nx">Migrations</span>
<span class="o">/</span><span class="nx">Seeds</span>
<span class="o">/</span><span class="nx">Helpers</span>
<span class="o">/</span><span class="nx">Language</span>
<span class="o">/</span><span class="nx">en</span>
<span class="o">/</span><span class="nx">Libraries</span>
<span class="o">/</span><span class="nx">Models</span>
<span class="o">/</span><span class="nx">Views</span>
</pre></div>
</div>
<p>Of course, there is nothing forcing you to use this exact structure, and you should organize it in the manner that
best suits your module, leaving out directories you don&#8217;t need, creating new directories for Entities, Interfaces,
or Repositories, etc.</p>
</div>
<div class="section" id="auto-discovery">
<h2><a class="toc-backref" href="#id2">Auto-Discovery</a><a class="headerlink" href="#auto-discovery" title="Permalink to this headline"></a></h2>
<p>Many times, you will need to specify the full namespace to files you want to include, but CodeIgniter can be
configured to make integrating modules into your applications simpler by automatically discovering many different
file types, including:</p>
<ul class="simple">
<li><a class="reference internal" href="../extending/events.html"><span class="doc">Events</span></a></li>
<li><a class="reference internal" href="configuration.html"><span class="doc">Registrars</span></a></li>
<li><a class="reference internal" href="../incoming/routing.html"><span class="doc">Route files</span></a></li>
<li><a class="reference internal" href="../concepts/services.html"><span class="doc">Services</span></a></li>
</ul>
<p>This is configured in the file <strong>application/Config/Modules.php</strong>.</p>
<p>The auto-discovery system works by scanning any psr4 namespaces that have been defined within <strong>Config/Autoload.php</strong>
for familiar directories/files.</p>
<p>When at the <strong>acme</strong> namespace above, we would need to make one small adjustment to make it so the files could be found:
each &#8220;module&#8221; within the namespace would have to have it&#8217;s own namespace defined there. <strong>Acme</strong> would be changed
to <strong>AcmeBlog</strong>. Once your module folder has been defined, the discover process would look for a Routes file, for example,
at <strong>/acme/Blog/Config/Routes.php</strong>, just as if it was another application.</p>
<div class="section" id="enable-disable-discover">
<h3><a class="toc-backref" href="#id3">Enable/Disable Discover</a><a class="headerlink" href="#enable-disable-discover" title="Permalink to this headline"></a></h3>
<p>You can turn on or off all auto-discovery in the system with the <strong>$enabled</strong> class variable. False will disable
all discovery, optimizing performance, but negating the special capabilities of your modules.</p>
</div>
<div class="section" id="specify-discovery-items">
<h3><a class="toc-backref" href="#id4">Specify Discovery Items</a><a class="headerlink" href="#specify-discovery-items" title="Permalink to this headline"></a></h3>
<p>With the <strong>$activeExplorers</strong> option, you can specify which items are automatically discovered. If the item is not
present, then no auto-discovery will happen for that item, but the others in the array will still be discovered.</p>
</div>
</div>
<div class="section" id="working-with-files">
<h2><a class="toc-backref" href="#id5">Working With Files</a><a class="headerlink" href="#working-with-files" title="Permalink to this headline"></a></h2>
<p>This section will take a look at each of the file types (controllers, views, language files, etc) and how they can
be used within the module. Some of this information is described in more detail in the relevant location of the user
guide, but is being reproduced here so that it&#8217;s easier to grasp how all of the pieces fit together.</p>
<div class="section" id="routes">
<h3><a class="toc-backref" href="#id6">Routes</a><a class="headerlink" href="#routes" title="Permalink to this headline"></a></h3>
<p>By default, <a class="reference internal" href="../incoming/routing.html"><span class="doc">routes</span></a> are automatically scanned for within modules. If can be turned off in
the <strong>Modules</strong> config file, described above.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Since the files are being included into the current scope, the <code class="docutils literal"><span class="pre">$routes</span></code> instance is already defined for you.
It will cause errors if you attempt to redefine that class.</p>
</div>
</div>
<div class="section" id="controllers">
<h3><a class="toc-backref" href="#id7">Controllers</a><a class="headerlink" href="#controllers" title="Permalink to this headline"></a></h3>
<p>Controllers outside of the main <strong>application/Controllers</strong> directory cannot be automatically routed by URI detection,
but must be specified within the Routes file itself:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// Routes.php</span>
<span class="nv">$routes</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;blog&#39;</span><span class="p">,</span> <span class="s1">&#39;Acme\Blog\Controllers\Blog::index&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>To reduce the amount of typing needed here, the <strong>group</strong> routing feature is helpful:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-&gt;</span><span class="na">group</span><span class="p">(</span><span class="s1">&#39;blog&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;namespace&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Acme\Blog\Controllers&#39;</span><span class="p">],</span> <span class="k">function</span><span class="p">(</span><span class="nv">$routes</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$routes</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;Blog::index&#39;</span><span class="p">);</span>
<span class="p">});</span>
</pre></div>
</div>
</div>
<div class="section" id="config-files">
<h3><a class="toc-backref" href="#id8">Config Files</a><a class="headerlink" href="#config-files" title="Permalink to this headline"></a></h3>
<p>No special change is needed when working with configuration files. These are still namespaced classes and loaded
with the <code class="docutils literal"><span class="pre">new</span></code> command:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$config</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Acme\Blog\Config\Blog</span><span class="p">();</span>
</pre></div>
</div>
<p>Config files are automatically discovered whenever using the <strong>config()</strong> function that is always available.</p>
</div>
<div class="section" id="migrations">
<h3><a class="toc-backref" href="#id9">Migrations</a><a class="headerlink" href="#migrations" title="Permalink to this headline"></a></h3>
<p>Migration files will be automatically discovered within defined namespaces. All migrations found across all
namespaces will be run every time.</p>
</div>
<div class="section" id="seeds">
<h3><a class="toc-backref" href="#id10">Seeds</a><a class="headerlink" href="#seeds" title="Permalink to this headline"></a></h3>
<p>Seed files can be used from both the CLI and called from within other seed files as long as the full namespace
is provided. If calling on the CLI, you will need to provide double backslashes:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&gt;</span> <span class="nx">php</span> <span class="k">public</span><span class="o">/</span><span class="nx">index</span><span class="o">.</span><span class="nx">php</span> <span class="nx">migrations</span> <span class="nx">seed</span> <span class="nx">Acme\\Blog\\Database\\Seeds\\TestPostSeeder</span>
</pre></div>
</div>
</div>
<div class="section" id="helpers">
<h3><a class="toc-backref" href="#id11">Helpers</a><a class="headerlink" href="#helpers" title="Permalink to this headline"></a></h3>
<p>Helpers will be located automatically from defined namespaces when using the <code class="docutils literal"><span class="pre">helper()</span></code> method, as long as it
is within the namespaces <strong>Helpers</strong> directory:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">helper</span><span class="p">(</span><span class="s1">&#39;blog&#39;</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="language-files">
<h3><a class="toc-backref" href="#id12">Language Files</a><a class="headerlink" href="#language-files" title="Permalink to this headline"></a></h3>
<p>Language files are located automatically from defined namespaces when using the <code class="docutils literal"><span class="pre">lang()</span></code> method, as long as the
file follows the same directory structures as the main application directory.</p>
</div>
<div class="section" id="libraries">
<h3><a class="toc-backref" href="#id13">Libraries</a><a class="headerlink" href="#libraries" title="Permalink to this headline"></a></h3>
<p>Libraries are always instantiated by their fully-qualified class name, so no special access is provided:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$lib</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Acme\Blog\Libraries\BlogLib</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="models">
<h3><a class="toc-backref" href="#id14">Models</a><a class="headerlink" href="#models" title="Permalink to this headline"></a></h3>
<p>Models are always instantiated by their fully-qualified class name, so no special access is provided:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$model</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Acme\Blog\Models\PostModel</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="views">
<h3><a class="toc-backref" href="#id15">Views</a><a class="headerlink" href="#views" title="Permalink to this headline"></a></h3>
<p>Views can be loaded using the class namespace as described in the <a class="reference internal" href="../outgoing/views.html"><span class="doc">views</span></a> documentation:</p>
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">&#39;Acme\Blog\Views\index&#39;</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="managing_apps.html" class="btn btn-neutral float-right" title="Managing your Applications" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="caching.html" class="btn btn-neutral" title="Web Page Caching" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2014-2018 British Columbia Institute of Technology.
Last updated on Nov 28, 2018.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../',
VERSION:'4.0.0-alpha.2',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: false,
SOURCELINK_SUFFIX: ''
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>