Update User Guide

This commit is contained in:
kenjis 2024-04-02 22:49:45 +00:00
parent 4fe7ebe2ec
commit aff7f45948
5 changed files with 307 additions and 251 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 85 KiB

After

Width:  |  Height:  |  Size: 121 KiB

View File

@ -294,35 +294,43 @@ is running under the <code class="docutils literal notranslate"><span class="pre
keep the best user experience for your users.</p>
<nav class="contents local" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#using-exceptions" id="id3">Using Exceptions</a></p></li>
<li><p><a class="reference internal" href="#configuration" id="id4">Configuration</a></p>
<li><p><a class="reference internal" href="#using-exceptions" id="id4">Using Exceptions</a></p>
<ul>
<li><p><a class="reference internal" href="#error-reporting" id="id5">Error Reporting</a></p></li>
<li><p><a class="reference internal" href="#logging-exceptions" id="id6">Logging Exceptions</a></p></li>
<li><p><a class="reference internal" href="#what-is-exceptions" id="id5">What is Exceptions</a></p></li>
<li><p><a class="reference internal" href="#catching-exceptions" id="id6">Catching Exceptions</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#framework-exceptions" id="id7">Framework Exceptions</a></p>
<li><p><a class="reference internal" href="#configuration" id="id7">Configuration</a></p>
<ul>
<li><p><a class="reference internal" href="#pagenotfoundexception" id="id8">PageNotFoundException</a></p></li>
<li><p><a class="reference internal" href="#configexception" id="id9">ConfigException</a></p></li>
<li><p><a class="reference internal" href="#databaseexception" id="id10">DatabaseException</a></p></li>
<li><p><a class="reference internal" href="#redirectexception" id="id11">RedirectException</a></p></li>
<li><p><a class="reference internal" href="#error-reporting" id="id8">Error Reporting</a></p></li>
<li><p><a class="reference internal" href="#logging-exceptions" id="id9">Logging Exceptions</a></p></li>
<li><p><a class="reference internal" href="#logging-deprecation-warnings" id="id10">Logging Deprecation Warnings</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#specify-http-status-code-in-your-exception" id="id12">Specify HTTP Status Code in Your Exception</a></p></li>
<li><p><a class="reference internal" href="#specify-exit-code-in-your-exception" id="id13">Specify Exit Code in Your Exception</a></p></li>
<li><p><a class="reference internal" href="#logging-deprecation-warnings" id="id14">Logging Deprecation Warnings</a></p></li>
<li><p><a class="reference internal" href="#custom-exception-handlers" id="id15">Custom Exception Handlers</a></p>
<li><p><a class="reference internal" href="#framework-exceptions" id="id11">Framework Exceptions</a></p>
<ul>
<li><p><a class="reference internal" href="#defining-the-new-handler" id="id16">Defining the New Handler</a></p></li>
<li><p><a class="reference internal" href="#configuring-the-new-handler" id="id17">Configuring the New Handler</a></p></li>
<li><p><a class="reference internal" href="#pagenotfoundexception" id="id12">PageNotFoundException</a></p></li>
<li><p><a class="reference internal" href="#configexception" id="id13">ConfigException</a></p></li>
<li><p><a class="reference internal" href="#databaseexception" id="id14">DatabaseException</a></p></li>
<li><p><a class="reference internal" href="#redirectexception" id="id15">RedirectException</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#specify-http-status-code-in-your-exception" id="id16">Specify HTTP Status Code in Your Exception</a></p></li>
<li><p><a class="reference internal" href="#http-status-code-and-error-views" id="id17">HTTP Status Code and Error Views</a></p></li>
<li><p><a class="reference internal" href="#specify-exit-code-in-your-exception" id="id18">Specify Exit Code in Your Exception</a></p></li>
<li><p><a class="reference internal" href="#custom-exception-handlers" id="id19">Custom Exception Handlers</a></p>
<ul>
<li><p><a class="reference internal" href="#defining-the-new-handler" id="id20">Defining the New Handler</a></p></li>
<li><p><a class="reference internal" href="#configuring-the-new-handler" id="id21">Configuring the New Handler</a></p></li>
</ul>
</li>
</ul>
</nav>
<section id="using-exceptions">
<h2><a class="toc-backref" href="#id3" role="doc-backlink">Using Exceptions</a><a class="headerlink" href="#using-exceptions" title="Permalink to this heading"></a></h2>
<h2><a class="toc-backref" href="#id4" role="doc-backlink">Using Exceptions</a><a class="headerlink" href="#using-exceptions" title="Permalink to this heading"></a></h2>
<p>This section is a quick overview for newer programmers, or for developers who are not experienced with using exceptions.</p>
<section id="what-is-exceptions">
<h3><a class="toc-backref" href="#id5" role="doc-backlink">What is Exceptions</a><a class="headerlink" href="#what-is-exceptions" title="Permalink to this heading"></a></h3>
<p>Exceptions are simply events that happen when the exception is “thrown”. This halts the current flow of the script, and
execution is then sent to the error handler which displays the appropriate error page:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -330,6 +338,9 @@ execution is then sent to the error handler which displays the appropriate error
<span class="k">throw</span> <span class="k">new</span> <span class="nx">\Exception</span><span class="p">(</span><span class="s1">&#39;Some message goes here&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="catching-exceptions">
<h3><a class="toc-backref" href="#id6" role="doc-backlink">Catching Exceptions</a><a class="headerlink" href="#catching-exceptions" title="Permalink to this heading"></a></h3>
<p>If you are calling a method that might throw an exception, you can catch that exception using a <code class="docutils literal notranslate"><span class="pre">try/catch</span></code> block:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -342,14 +353,18 @@ execution is then sent to the error handler which displays the appropriate error
</div>
<p>If the <code class="docutils literal notranslate"><span class="pre">$userModel</span></code> throws an exception, it is caught and the code within the catch block is executed. In this example,
the scripts dies, echoing the error message that the <code class="docutils literal notranslate"><span class="pre">UserModel</span></code> defined.</p>
<section id="catching-specific-exceptions">
<h4>Catching Specific Exceptions<a class="headerlink" href="#catching-specific-exceptions" title="Permalink to this heading"></a></h4>
<p>In the example above, we catch any type of Exception. If we only want to watch for specific types of exceptions, like
a <code class="docutils literal notranslate"><span class="pre">UnknownFileException</span></code>, we can specify that in the catch parameter. Any other exceptions that are thrown and are
a <code class="docutils literal notranslate"><span class="pre">DataException</span></code>, we can specify that in the catch parameter. Any other exceptions that are thrown and are
not child classes of the caught exception will be passed on to the error handler:</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">use</span> <span class="nx">CodeIgniter\Database\Exceptions\DataException</span><span class="p">;</span>
<span class="k">try</span> <span class="p">{</span>
<span class="nv">$user</span> <span class="o">=</span> <span class="nv">$userModel</span><span class="o">-&gt;</span><span class="na">find</span><span class="p">(</span><span class="nv">$id</span><span class="p">);</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">\CodeIgniter\UnknownFileException</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">DataException</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// do something here...</span>
<span class="p">}</span>
</pre></div>
@ -358,9 +373,11 @@ not child classes of the caught exception will be passed on to the error handler
the error handler to function as normal, you can throw a new exception within the catch block:</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">use</span> <span class="nx">CodeIgniter\Database\Exceptions\DataException</span><span class="p">;</span>
<span class="k">try</span> <span class="p">{</span>
<span class="nv">$user</span> <span class="o">=</span> <span class="nv">$userModel</span><span class="o">-&gt;</span><span class="na">find</span><span class="p">(</span><span class="nv">$id</span><span class="p">);</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">\CodeIgniter\UnknownFileException</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">DataException</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// do something here...</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">\RuntimeException</span><span class="p">(</span><span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">(),</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getCode</span><span class="p">(),</span> <span class="nv">$e</span><span class="p">);</span>
@ -368,10 +385,12 @@ the error handler to function as normal, you can throw a new exception within th
</pre></div>
</div>
</section>
</section>
</section>
<section id="configuration">
<h2><a class="toc-backref" href="#id4" role="doc-backlink">Configuration</a><a class="headerlink" href="#configuration" title="Permalink to this heading"></a></h2>
<h2><a class="toc-backref" href="#id7" role="doc-backlink">Configuration</a><a class="headerlink" href="#configuration" title="Permalink to this heading"></a></h2>
<section id="error-reporting">
<h3><a class="toc-backref" href="#id5" role="doc-backlink">Error Reporting</a><a class="headerlink" href="#error-reporting" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id8" role="doc-backlink">Error Reporting</a><a class="headerlink" href="#error-reporting" title="Permalink to this heading"></a></h3>
<p>By default, CodeIgniter will display a detailed error report with all errors in the <code class="docutils literal notranslate"><span class="pre">development</span></code> and <code class="docutils literal notranslate"><span class="pre">testing</span></code> environments, and will not
display any errors in the <code class="docutils literal notranslate"><span class="pre">production</span></code> environment.</p>
<img alt="../_images/error.png" src="../_images/error.png" />
@ -389,8 +408,8 @@ is displayed, <strong>your secure credentials are publicly exposed</strong>.</p>
</div>
</section>
<section id="logging-exceptions">
<h3><a class="toc-backref" href="#id6" role="doc-backlink">Logging Exceptions</a><a class="headerlink" href="#logging-exceptions" title="Permalink to this heading"></a></h3>
<p>By default, all Exceptions other than 404 - Page Not Found exceptions are logged. This can be turned on and off
<h3><a class="toc-backref" href="#id9" role="doc-backlink">Logging Exceptions</a><a class="headerlink" href="#logging-exceptions" title="Permalink to this heading"></a></h3>
<p>By default, all Exceptions other than 404 - Page Not Found exceptions are logged. This can be turned on and off
by setting the <code class="docutils literal notranslate"><span class="pre">$log</span></code> value of <strong>app/Config/Exceptions.php</strong>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -400,7 +419,9 @@ by setting the <code class="docutils literal notranslate"><span class="pre">$log
<span class="k">class</span> <span class="nc">Exceptions</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
<span class="p">{</span>
<span class="k">public</span> <span class="nv">$log</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="k">public</span> <span class="nx">bool</span> <span class="nv">$log</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
@ -413,113 +434,21 @@ by setting the <code class="docutils literal notranslate"><span class="pre">$log
<span class="k">class</span> <span class="nc">Exceptions</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
<span class="p">{</span>
<span class="k">public</span> <span class="nv">$ignoredCodes</span> <span class="o">=</span> <span class="p">[</span><span class="mi">404</span><span class="p">];</span>
<span class="c1">// ...</span>
<span class="k">public</span> <span class="k">array</span> <span class="nv">$ignoreCodes</span> <span class="o">=</span> <span class="p">[</span><span class="mi">404</span><span class="p">];</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>It is possible that logging still will not happen for exceptions if your current Log settings
are not set up to log <strong>critical</strong> errors, which all exceptions are logged as.</p>
<p>It is possible that logging still will not happen for exceptions if your current
<a class="reference internal" href="logging.html#logging-configuration"><span class="std std-ref">Log settings</span></a>
are not set up to log <code class="docutils literal notranslate"><span class="pre">critical</span></code> errors, which all exceptions are logged as.</p>
</div>
</section>
</section>
<section id="framework-exceptions">
<h2><a class="toc-backref" href="#id7" role="doc-backlink">Framework Exceptions</a><a class="headerlink" href="#framework-exceptions" title="Permalink to this heading"></a></h2>
<p>The following framework exceptions are available:</p>
<section id="pagenotfoundexception">
<h3><a class="toc-backref" href="#id8" role="doc-backlink">PageNotFoundException</a><a class="headerlink" href="#pagenotfoundexception" title="Permalink to this heading"></a></h3>
<p>This is used to signal a 404, Page Not Found error. When thrown, the system will show the view found at
<strong>app/Views/errors/html/error_404.php</strong>. You should customize all of the error views for your site.
If, in <strong>app/Config/Routes.php</strong>, you have specified a 404 Override, that will be called instead of the standard
404 page:</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">if</span> <span class="p">(</span><span class="o">!</span> <span class="nv">$page</span> <span class="o">=</span> <span class="nv">$pageModel</span><span class="o">-&gt;</span><span class="na">find</span><span class="p">(</span><span class="nv">$id</span><span class="p">))</span> <span class="p">{</span>
<span class="k">throw</span> <span class="nx">\CodeIgniter\Exceptions\PageNotFoundException</span><span class="o">::</span><span class="na">forPageNotFound</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You can pass a message into the exception that will be displayed in place of the default message on the 404 page.</p>
</section>
<section id="configexception">
<h3><a class="toc-backref" href="#id9" role="doc-backlink">ConfigException</a><a class="headerlink" href="#configexception" title="Permalink to this heading"></a></h3>
<p>This exception should be used when the values from the configuration class are invalid, or when the config class
is not the right type, etc:</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">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\Exceptions\ConfigException</span><span class="p">();</span>
</pre></div>
</div>
<p>This provides an exit code of 3.</p>
</section>
<section id="databaseexception">
<h3><a class="toc-backref" href="#id10" role="doc-backlink">DatabaseException</a><a class="headerlink" href="#databaseexception" title="Permalink to this heading"></a></h3>
<p>This exception is thrown for database errors, such as when the database connection cannot be created,
or when it is temporarily lost:</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">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\Database\Exceptions\DatabaseException</span><span class="p">();</span>
</pre></div>
</div>
<p>This provides an exit code of 8.</p>
</section>
<section id="redirectexception">
<h3><a class="toc-backref" href="#id11" role="doc-backlink">RedirectException</a><a class="headerlink" href="#redirectexception" title="Permalink to this heading"></a></h3>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Since v4.4.0, the namespace of <code class="docutils literal notranslate"><span class="pre">RedirectException</span></code> has been changed.
Previously it was <code class="docutils literal notranslate"><span class="pre">CodeIgniter\Router\Exceptions\RedirectException</span></code>. The
previous class is deprecated.</p>
</div>
<p>This exception is a special case allowing for overriding of all other response routing and
forcing a redirect to a specific URI:</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">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\HTTP\Exceptions\RedirectException</span><span class="p">(</span><span class="nv">$uri</span><span class="p">);</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">$uri</span></code> is a URI path relative to baseURL. You can also supply a
redirect code to use instead of the default (<code class="docutils literal notranslate"><span class="pre">302</span></code>, “temporary redirect”):</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">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\HTTP\Exceptions\RedirectException</span><span class="p">(</span><span class="nv">$uri</span><span class="p">,</span> <span class="mi">301</span><span class="p">);</span>
</pre></div>
</div>
<p>Also, since v4.4.0 an object of a class that implements ResponseInterface can be used as the first argument.
This solution is suitable for cases where you need to add additional headers or cookies in the response.</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">$response</span> <span class="o">=</span> <span class="nx">\Config\Services</span><span class="o">::</span><span class="na">response</span><span class="p">()</span>
<span class="o">-&gt;</span><span class="na">redirect</span><span class="p">(</span><span class="s1">&#39;https://example.com/path&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">setHeader</span><span class="p">(</span><span class="s1">&#39;Some&#39;</span><span class="p">,</span> <span class="s1">&#39;header&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">setCookie</span><span class="p">(</span><span class="s1">&#39;and&#39;</span><span class="p">,</span> <span class="s1">&#39;cookie&#39;</span><span class="p">);</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\HTTP\Exceptions\RedirectException</span><span class="p">(</span><span class="nv">$response</span><span class="p">);</span>
</pre></div>
</div>
</section>
</section>
<section id="specify-http-status-code-in-your-exception">
<span id="error-specify-http-status-code"></span><h2><a class="toc-backref" href="#id12" role="doc-backlink">Specify HTTP Status Code in Your Exception</a><a class="headerlink" href="#specify-http-status-code-in-your-exception" title="Permalink to this heading"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.3.0.</span></p>
</div>
<p>Since v4.3.0, you can specify the HTTP status code for your Exception class to implement
<code class="docutils literal notranslate"><span class="pre">HTTPExceptionInterface</span></code>.</p>
<p>When an exception implementing <code class="docutils literal notranslate"><span class="pre">HTTPExceptionInterface</span></code> is caught by CodeIgniters exception handler, the Exception code will become the HTTP status code.</p>
</section>
<section id="specify-exit-code-in-your-exception">
<span id="error-specify-exit-code"></span><h2><a class="toc-backref" href="#id13" role="doc-backlink">Specify Exit Code in Your Exception</a><a class="headerlink" href="#specify-exit-code-in-your-exception" title="Permalink to this heading"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.3.0.</span></p>
</div>
<p>Since v4.3.0, you can specify the exit code for your Exception class to implement
<code class="docutils literal notranslate"><span class="pre">HasExitCodeInterface</span></code>.</p>
<p>When an exception implementing <code class="docutils literal notranslate"><span class="pre">HasExitCodeInterface</span></code> is caught by CodeIgniters exception handler, the code returned from the <code class="docutils literal notranslate"><span class="pre">getExitCode()</span></code> method will become the exit code.</p>
</section>
<section id="logging-deprecation-warnings">
<span id="id1"></span><h2><a class="toc-backref" href="#id14" role="doc-backlink">Logging Deprecation Warnings</a><a class="headerlink" href="#logging-deprecation-warnings" title="Permalink to this heading"></a></h2>
<span id="id1"></span><h3><a class="toc-backref" href="#id10" role="doc-backlink">Logging Deprecation Warnings</a><a class="headerlink" href="#logging-deprecation-warnings" title="Permalink to this heading"></a></h3>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.3.0.</span></p>
</div>
@ -572,15 +501,142 @@ it accordingly.</p>
<p>For testing your application you may want to always throw on deprecations. You may configure this by
setting the environment variable <code class="docutils literal notranslate"><span class="pre">CODEIGNITER_SCREAM_DEPRECATIONS</span></code> to a truthy value.</p>
</section>
</section>
<section id="framework-exceptions">
<h2><a class="toc-backref" href="#id11" role="doc-backlink">Framework Exceptions</a><a class="headerlink" href="#framework-exceptions" title="Permalink to this heading"></a></h2>
<p>The following framework exceptions are available:</p>
<section id="pagenotfoundexception">
<h3><a class="toc-backref" href="#id12" role="doc-backlink">PageNotFoundException</a><a class="headerlink" href="#pagenotfoundexception" title="Permalink to this heading"></a></h3>
<p>This is used to signal a 404, Page Not Found error:</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">use</span> <span class="nx">CodeIgniter\Exceptions\PageNotFoundException</span><span class="p">;</span>
<span class="nv">$page</span> <span class="o">=</span> <span class="nv">$pageModel</span><span class="o">-&gt;</span><span class="na">find</span><span class="p">(</span><span class="nv">$id</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$page</span> <span class="o">===</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="nx">PageNotFoundException</span><span class="o">::</span><span class="na">forPageNotFound</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You can pass a message into the exception that will be displayed in place of the default message on the 404 page.</p>
<p>For the default 404 view file location, see <a class="reference internal" href="#http-status-code-and-error-views"><span class="std std-ref">HTTP Status Code and Error Views</span></a>.</p>
<p>If, in <strong>app/Config/Routing.php</strong> or <strong>app/Config/Routes.php</strong>, you have specified
a <a class="reference internal" href="../incoming/routing.html#override"><span class="std std-ref">404 Override</span></a>, that will be called instead of the standard 404 page.</p>
</section>
<section id="configexception">
<h3><a class="toc-backref" href="#id13" role="doc-backlink">ConfigException</a><a class="headerlink" href="#configexception" title="Permalink to this heading"></a></h3>
<p>This exception should be used when the values from the configuration class are invalid, or when the config class
is not the right type, etc:</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">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\Exceptions\ConfigException</span><span class="p">();</span>
</pre></div>
</div>
<p>This provides an exit code of 3.</p>
</section>
<section id="databaseexception">
<h3><a class="toc-backref" href="#id14" role="doc-backlink">DatabaseException</a><a class="headerlink" href="#databaseexception" title="Permalink to this heading"></a></h3>
<p>This exception is thrown for database errors, such as when the database connection cannot be created,
or when it is temporarily lost:</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">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\Database\Exceptions\DatabaseException</span><span class="p">();</span>
</pre></div>
</div>
<p>This provides an exit code of 8.</p>
</section>
<section id="redirectexception">
<h3><a class="toc-backref" href="#id15" role="doc-backlink">RedirectException</a><a class="headerlink" href="#redirectexception" title="Permalink to this heading"></a></h3>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Since v4.4.0, the namespace of <code class="docutils literal notranslate"><span class="pre">RedirectException</span></code> has been changed.
Previously it was <code class="docutils literal notranslate"><span class="pre">CodeIgniter\Router\Exceptions\RedirectException</span></code>. The
previous class is deprecated.</p>
</div>
<p>This exception is a special case allowing for overriding of all other response routing and
forcing a redirect to a specific URI:</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">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\HTTP\Exceptions\RedirectException</span><span class="p">(</span><span class="nv">$uri</span><span class="p">);</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">$uri</span></code> is a URI path relative to baseURL. You can also supply a
redirect code to use instead of the default (<code class="docutils literal notranslate"><span class="pre">302</span></code>, “temporary redirect”):</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">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\HTTP\Exceptions\RedirectException</span><span class="p">(</span><span class="nv">$uri</span><span class="p">,</span> <span class="mi">301</span><span class="p">);</span>
</pre></div>
</div>
<p>Also, since v4.4.0 an object of a class that implements ResponseInterface can be used as the first argument.
This solution is suitable for cases where you need to add additional headers or cookies in the response.</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">$response</span> <span class="o">=</span> <span class="nx">\Config\Services</span><span class="o">::</span><span class="na">response</span><span class="p">()</span>
<span class="o">-&gt;</span><span class="na">redirect</span><span class="p">(</span><span class="s1">&#39;https://example.com/path&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">setHeader</span><span class="p">(</span><span class="s1">&#39;Some&#39;</span><span class="p">,</span> <span class="s1">&#39;header&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">setCookie</span><span class="p">(</span><span class="s1">&#39;and&#39;</span><span class="p">,</span> <span class="s1">&#39;cookie&#39;</span><span class="p">);</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">\CodeIgniter\HTTP\Exceptions\RedirectException</span><span class="p">(</span><span class="nv">$response</span><span class="p">);</span>
</pre></div>
</div>
</section>
</section>
<section id="specify-http-status-code-in-your-exception">
<span id="error-specify-http-status-code"></span><h2><a class="toc-backref" href="#id16" role="doc-backlink">Specify HTTP Status Code in Your Exception</a><a class="headerlink" href="#specify-http-status-code-in-your-exception" title="Permalink to this heading"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.3.0.</span></p>
</div>
<p>Since v4.3.0, you can specify the HTTP status code for your Exception class to implement
<code class="docutils literal notranslate"><span class="pre">CodeIgniter\Exceptions\HTTPExceptionInterface</span></code>.</p>
<p>When an exception implementing <code class="docutils literal notranslate"><span class="pre">HTTPExceptionInterface</span></code> is caught by CodeIgniters exception handler, the Exception code will become the HTTP status code.</p>
</section>
<section id="http-status-code-and-error-views">
<span id="id2"></span><h2><a class="toc-backref" href="#id17" role="doc-backlink">HTTP Status Code and Error Views</a><a class="headerlink" href="#http-status-code-and-error-views" title="Permalink to this heading"></a></h2>
<p>The exception handler displays the error view corresponding to the HTTP status
code, if one exists.</p>
<p>For example, <code class="docutils literal notranslate"><span class="pre">PageNotFoundException</span></code> implements the <code class="docutils literal notranslate"><span class="pre">HTTPExceptionInterface</span></code>,
so its exception code <code class="docutils literal notranslate"><span class="pre">404</span></code> will be the HTTP status code. Therefore if it is
thrown, the system will show the <strong>error_404.php</strong> in the <strong>app/Views/errors/html</strong>
folder when it is a web request. If it is invoked via CLI, the system will show
the <strong>error_404.php</strong> in the <strong>app/Views/errors/cli</strong> folder.</p>
<p>If there is no view file corresponding to the HTTP status code, <strong>production.php</strong>
or <strong>error_exception.php</strong> will be displayed.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If <code class="docutils literal notranslate"><span class="pre">display_errors</span></code> is on in the PHP INI configuration,
<strong>error_exception.php</strong> is selected and a detailed error report is displayed.</p>
</div>
<p>You should customize all of the error views in the <strong>app/Views/errors/html</strong> folder
for your site.</p>
<p>You can also create error views for specific HTTP status code. For example, if
you want to create an error view for “400 Bad Request”, add <strong>error_400.php</strong>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>If an error view file with the corresponding HTTP status code exists,
the exception handler will display that file regardless of the environment.
The view file must be implemented in such a way that it does not display
detailed error messages in production environment by yourself.</p>
</div>
</section>
<section id="specify-exit-code-in-your-exception">
<span id="error-specify-exit-code"></span><h2><a class="toc-backref" href="#id18" role="doc-backlink">Specify Exit Code in Your Exception</a><a class="headerlink" href="#specify-exit-code-in-your-exception" title="Permalink to this heading"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.3.0.</span></p>
</div>
<p>Since v4.3.0, you can specify the exit code for your Exception class to implement
<code class="docutils literal notranslate"><span class="pre">CodeIgniter\Exceptions\HasExitCodeInterface</span></code>.</p>
<p>When an exception implementing <code class="docutils literal notranslate"><span class="pre">HasExitCodeInterface</span></code> is caught by CodeIgniters exception handler, the code returned from the <code class="docutils literal notranslate"><span class="pre">getExitCode()</span></code> method will become the exit code.</p>
</section>
<section id="custom-exception-handlers">
<span id="id2"></span><h2><a class="toc-backref" href="#id15" role="doc-backlink">Custom Exception Handlers</a><a class="headerlink" href="#custom-exception-handlers" title="Permalink to this heading"></a></h2>
<span id="id3"></span><h2><a class="toc-backref" href="#id19" role="doc-backlink">Custom Exception Handlers</a><a class="headerlink" href="#custom-exception-handlers" title="Permalink to this heading"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.4.0.</span></p>
</div>
<p>If you need more control over how exceptions are displayed you can now define your own handlers and
specify when they apply.</p>
<section id="defining-the-new-handler">
<h3><a class="toc-backref" href="#id16" role="doc-backlink">Defining the New Handler</a><a class="headerlink" href="#defining-the-new-handler" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id20" role="doc-backlink">Defining the New Handler</a><a class="headerlink" href="#defining-the-new-handler" title="Permalink to this heading"></a></h3>
<p>The first step is to create a new class which implements <code class="docutils literal notranslate"><span class="pre">CodeIgniter\Debug\ExceptionHandlerInterface</span></code>.
You can also extend <code class="docutils literal notranslate"><span class="pre">CodeIgniter\Debug\BaseExceptionHandler</span></code>.
This class includes a number of utility methods that are used by the default exception handler.
@ -618,7 +674,7 @@ The new handler must implement a single method: <code class="docutils literal no
exit code. However, the <code class="docutils literal notranslate"><span class="pre">BaseExceptionHandler</span></code> provides a number of other helper functions and objects.</p>
</section>
<section id="configuring-the-new-handler">
<h3><a class="toc-backref" href="#id17" role="doc-backlink">Configuring the New Handler</a><a class="headerlink" href="#configuring-the-new-handler" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id21" role="doc-backlink">Configuring the New Handler</a><a class="headerlink" href="#configuring-the-new-handler" title="Permalink to this heading"></a></h3>
<p>Telling CodeIgniter to use your new exception handler class is done in the <strong>app/Config/Exceptions.php</strong>
configuration files <code class="docutils literal notranslate"><span class="pre">handler()</span></code> method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>

View File

@ -288,105 +288,105 @@
<h1>URI Routing<a class="headerlink" href="#uri-routing" title="Permalink to this heading"></a></h1>
<nav class="contents local" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#what-is-uri-routing" id="id13">What is URI Routing?</a></p></li>
<li><p><a class="reference internal" href="#setting-routing-rules" id="id14">Setting Routing Rules</a></p>
<li><p><a class="reference internal" href="#what-is-uri-routing" id="id14">What is URI Routing?</a></p></li>
<li><p><a class="reference internal" href="#setting-routing-rules" id="id15">Setting Routing Rules</a></p>
<ul>
<li><p><a class="reference internal" href="#examples" id="id15">Examples</a></p></li>
<li><p><a class="reference internal" href="#http-verb-routes" id="id16">HTTP verb Routes</a></p></li>
<li><p><a class="reference internal" href="#specifying-route-handlers" id="id17">Specifying Route Handlers</a></p>
<li><p><a class="reference internal" href="#examples" id="id16">Examples</a></p></li>
<li><p><a class="reference internal" href="#http-verb-routes" id="id17">HTTP verb Routes</a></p></li>
<li><p><a class="reference internal" href="#specifying-route-handlers" id="id18">Specifying Route Handlers</a></p>
<ul>
<li><p><a class="reference internal" href="#controller-s-namespace" id="id18">Controllers Namespace</a></p></li>
<li><p><a class="reference internal" href="#array-callable-syntax" id="id19">Array Callable Syntax</a></p></li>
<li><p><a class="reference internal" href="#using-closures" id="id20">Using Closures</a></p></li>
<li><p><a class="reference internal" href="#controller-s-namespace" id="id19">Controllers Namespace</a></p></li>
<li><p><a class="reference internal" href="#array-callable-syntax" id="id20">Array Callable Syntax</a></p></li>
<li><p><a class="reference internal" href="#using-closures" id="id21">Using Closures</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#specifying-route-paths" id="id21">Specifying Route Paths</a></p>
<li><p><a class="reference internal" href="#specifying-route-paths" id="id22">Specifying Route Paths</a></p>
<ul>
<li><p><a class="reference internal" href="#placeholders" id="id22">Placeholders</a></p></li>
<li><p><a class="reference internal" href="#custom-placeholders" id="id23">Custom Placeholders</a></p></li>
<li><p><a class="reference internal" href="#regular-expressions" id="id24">Regular Expressions</a></p></li>
<li><p><a class="reference internal" href="#placeholders" id="id23">Placeholders</a></p></li>
<li><p><a class="reference internal" href="#custom-placeholders" id="id24">Custom Placeholders</a></p></li>
<li><p><a class="reference internal" href="#regular-expressions" id="id25">Regular Expressions</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#view-routes" id="id25">View Routes</a></p></li>
<li><p><a class="reference internal" href="#redirecting-routes" id="id26">Redirecting Routes</a></p></li>
<li><p><a class="reference internal" href="#environment-restrictions" id="id27">Environment Restrictions</a></p></li>
<li><p><a class="reference internal" href="#routes-with-any-http-verbs" id="id28">Routes with any HTTP verbs</a></p></li>
<li><p><a class="reference internal" href="#mapping-multiple-routes" id="id29">Mapping Multiple Routes</a></p></li>
<li><p><a class="reference internal" href="#command-line-only-routes" id="id30">Command-Line Only Routes</a></p></li>
<li><p><a class="reference internal" href="#view-routes" id="id26">View Routes</a></p></li>
<li><p><a class="reference internal" href="#redirecting-routes" id="id27">Redirecting Routes</a></p></li>
<li><p><a class="reference internal" href="#environment-restrictions" id="id28">Environment Restrictions</a></p></li>
<li><p><a class="reference internal" href="#routes-with-any-http-verbs" id="id29">Routes with any HTTP verbs</a></p></li>
<li><p><a class="reference internal" href="#mapping-multiple-routes" id="id30">Mapping Multiple Routes</a></p></li>
<li><p><a class="reference internal" href="#command-line-only-routes" id="id31">Command-Line Only Routes</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#global-options" id="id31">Global Options</a></p>
<li><p><a class="reference internal" href="#global-options" id="id32">Global Options</a></p>
<ul>
<li><p><a class="reference internal" href="#applying-filters" id="id32">Applying Filters</a></p>
<li><p><a class="reference internal" href="#applying-filters" id="id33">Applying Filters</a></p>
<ul>
<li><p><a class="reference internal" href="#alias-filter" id="id33">Alias Filter</a></p></li>
<li><p><a class="reference internal" href="#classname-filter" id="id34">Classname Filter</a></p></li>
<li><p><a class="reference internal" href="#multiple-filters" id="id35">Multiple Filters</a></p></li>
<li><p><a class="reference internal" href="#alias-filter" id="id34">Alias Filter</a></p></li>
<li><p><a class="reference internal" href="#classname-filter" id="id35">Classname Filter</a></p></li>
<li><p><a class="reference internal" href="#multiple-filters" id="id36">Multiple Filters</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#assigning-namespace" id="id36">Assigning Namespace</a></p></li>
<li><p><a class="reference internal" href="#limit-to-hostname" id="id37">Limit to Hostname</a></p></li>
<li><p><a class="reference internal" href="#limit-to-subdomains" id="id38">Limit to Subdomains</a></p></li>
<li><p><a class="reference internal" href="#offsetting-the-matched-parameters" id="id39">Offsetting the Matched Parameters</a></p></li>
<li><p><a class="reference internal" href="#assigning-namespace" id="id37">Assigning Namespace</a></p></li>
<li><p><a class="reference internal" href="#limit-to-hostname" id="id38">Limit to Hostname</a></p></li>
<li><p><a class="reference internal" href="#limit-to-subdomains" id="id39">Limit to Subdomains</a></p></li>
<li><p><a class="reference internal" href="#offsetting-the-matched-parameters" id="id40">Offsetting the Matched Parameters</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#reverse-routing" id="id40">Reverse Routing</a></p></li>
<li><p><a class="reference internal" href="#named-routes" id="id41">Named Routes</a></p></li>
<li><p><a class="reference internal" href="#grouping-routes" id="id42">Grouping Routes</a></p>
<li><p><a class="reference internal" href="#reverse-routing" id="id41">Reverse Routing</a></p></li>
<li><p><a class="reference internal" href="#named-routes" id="id42">Named Routes</a></p></li>
<li><p><a class="reference internal" href="#grouping-routes" id="id43">Grouping Routes</a></p>
<ul>
<li><p><a class="reference internal" href="#setting-namespace" id="id43">Setting Namespace</a></p></li>
<li><p><a class="reference internal" href="#setting-filters" id="id44">Setting Filters</a></p></li>
<li><p><a class="reference internal" href="#setting-other-options" id="id45">Setting Other Options</a></p></li>
<li><p><a class="reference internal" href="#nesting-groups" id="id46">Nesting Groups</a></p></li>
<li><p><a class="reference internal" href="#setting-namespace" id="id44">Setting Namespace</a></p></li>
<li><p><a class="reference internal" href="#setting-filters" id="id45">Setting Filters</a></p></li>
<li><p><a class="reference internal" href="#setting-other-options" id="id46">Setting Other Options</a></p></li>
<li><p><a class="reference internal" href="#nesting-groups" id="id47">Nesting Groups</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#route-priority" id="id47">Route Priority</a></p>
<li><p><a class="reference internal" href="#route-priority" id="id48">Route Priority</a></p>
<ul>
<li><p><a class="reference internal" href="#changing-route-priority" id="id48">Changing Route Priority</a></p></li>
<li><p><a class="reference internal" href="#changing-route-priority" id="id49">Changing Route Priority</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#routes-configuration-options" id="id49">Routes Configuration Options</a></p>
<li><p><a class="reference internal" href="#routes-configuration-options" id="id50">Routes Configuration Options</a></p>
<ul>
<li><p><a class="reference internal" href="#default-namespace" id="id50">Default Namespace</a></p></li>
<li><p><a class="reference internal" href="#translate-uri-dashes" id="id51">Translate URI Dashes</a></p></li>
<li><p><a class="reference internal" href="#use-defined-routes-only" id="id52">Use Defined Routes Only</a></p></li>
<li><p><a class="reference internal" href="#override" id="id53">404 Override</a></p></li>
<li><p><a class="reference internal" href="#route-processing-by-priority" id="id54">Route Processing by Priority</a></p></li>
<li><p><a class="reference internal" href="#default-namespace" id="id51">Default Namespace</a></p></li>
<li><p><a class="reference internal" href="#translate-uri-dashes" id="id52">Translate URI Dashes</a></p></li>
<li><p><a class="reference internal" href="#use-defined-routes-only" id="id53">Use Defined Routes Only</a></p></li>
<li><p><a class="reference internal" href="#override" id="id54">404 Override</a></p></li>
<li><p><a class="reference internal" href="#route-processing-by-priority" id="id55">Route Processing by Priority</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#auto-routing-improved" id="id55">Auto Routing (Improved)</a></p>
<li><p><a class="reference internal" href="#auto-routing-improved" id="id56">Auto Routing (Improved)</a></p>
<ul>
<li><p><a class="reference internal" href="#enable-auto-routing" id="id56">Enable Auto Routing</a></p></li>
<li><p><a class="reference internal" href="#uri-segments" id="id57">URI Segments</a></p></li>
<li><p><a class="reference internal" href="#configuration-options" id="id58">Configuration Options</a></p>
<li><p><a class="reference internal" href="#enable-auto-routing" id="id57">Enable Auto Routing</a></p></li>
<li><p><a class="reference internal" href="#uri-segments" id="id58">URI Segments</a></p></li>
<li><p><a class="reference internal" href="#configuration-options" id="id59">Configuration Options</a></p>
<ul>
<li><p><a class="reference internal" href="#default-controller" id="id59">Default Controller</a></p></li>
<li><p><a class="reference internal" href="#default-method" id="id60">Default Method</a></p></li>
<li><p><a class="reference internal" href="#default-controller" id="id60">Default Controller</a></p></li>
<li><p><a class="reference internal" href="#default-method" id="id61">Default Method</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#module-routing" id="id61">Module Routing</a></p></li>
<li><p><a class="reference internal" href="#module-routing" id="id62">Module Routing</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#auto-routing-legacy" id="id62">Auto Routing (Legacy)</a></p>
<li><p><a class="reference internal" href="#auto-routing-legacy" id="id63">Auto Routing (Legacy)</a></p>
<ul>
<li><p><a class="reference internal" href="#enable-auto-routing-legacy" id="id63">Enable Auto Routing (Legacy)</a></p></li>
<li><p><a class="reference internal" href="#uri-segments-legacy" id="id64">URI Segments (Legacy)</a></p></li>
<li><p><a class="reference internal" href="#configuration-options-legacy" id="id65">Configuration Options (Legacy)</a></p>
<li><p><a class="reference internal" href="#enable-auto-routing-legacy" id="id64">Enable Auto Routing (Legacy)</a></p></li>
<li><p><a class="reference internal" href="#uri-segments-legacy" id="id65">URI Segments (Legacy)</a></p></li>
<li><p><a class="reference internal" href="#configuration-options-legacy" id="id66">Configuration Options (Legacy)</a></p>
<ul>
<li><p><a class="reference internal" href="#default-controller-legacy" id="id66">Default Controller (Legacy)</a></p></li>
<li><p><a class="reference internal" href="#default-method-legacy" id="id67">Default Method (Legacy)</a></p></li>
<li><p><a class="reference internal" href="#default-controller-legacy" id="id67">Default Controller (Legacy)</a></p></li>
<li><p><a class="reference internal" href="#default-method-legacy" id="id68">Default Method (Legacy)</a></p></li>
</ul>
</li>
</ul>
</li>
<li><p><a class="reference internal" href="#confirming-routes" id="id68">Confirming Routes</a></p>
<li><p><a class="reference internal" href="#confirming-routes" id="id69">Confirming Routes</a></p>
<ul>
<li><p><a class="reference internal" href="#spark-routes" id="id69">spark routes</a></p>
<li><p><a class="reference internal" href="#spark-routes" id="id70">spark routes</a></p>
<ul>
<li><p><a class="reference internal" href="#id11" id="id70">Auto Routing (Improved)</a></p></li>
<li><p><a class="reference internal" href="#id12" id="id71">Auto Routing (Legacy)</a></p></li>
<li><p><a class="reference internal" href="#sort-by-handler" id="id72">Sort by Handler</a></p></li>
<li><p><a class="reference internal" href="#specify-host" id="id73">Specify Host</a></p></li>
<li><p><a class="reference internal" href="#id12" id="id71">Auto Routing (Improved)</a></p></li>
<li><p><a class="reference internal" href="#id13" id="id72">Auto Routing (Legacy)</a></p></li>
<li><p><a class="reference internal" href="#sort-by-handler" id="id73">Sort by Handler</a></p></li>
<li><p><a class="reference internal" href="#specify-host" id="id74">Specify Host</a></p></li>
</ul>
</li>
</ul>
@ -394,7 +394,7 @@
</ul>
</nav>
<section id="what-is-uri-routing">
<h2><a class="toc-backref" href="#id13" role="doc-backlink">What is URI Routing?</a><a class="headerlink" href="#what-is-uri-routing" title="Permalink to this heading"></a></h2>
<h2><a class="toc-backref" href="#id14" role="doc-backlink">What is URI Routing?</a><a class="headerlink" href="#what-is-uri-routing" title="Permalink to this heading"></a></h2>
<p>URI Routing associates a URI with a controllers method.</p>
<p>CodeIgniter has two kinds of routing. One is <strong>Defined Route Routing</strong>, and the other is <strong>Auto Routing</strong>.
With Defined Route Routing, you can define routes manually. It allows flexible URL.
@ -402,7 +402,7 @@ Auto Routing automatically routes HTTP requests based on conventions and execute
<p>First, lets look at Defined Route Routing. If you want to use Auto Routing, see <a class="reference internal" href="#auto-routing-improved"><span class="std std-ref">Auto Routing (Improved)</span></a>.</p>
</section>
<section id="setting-routing-rules">
<span id="defined-route-routing"></span><h2><a class="toc-backref" href="#id14" role="doc-backlink">Setting Routing Rules</a><a class="headerlink" href="#setting-routing-rules" title="Permalink to this heading"></a></h2>
<span id="defined-route-routing"></span><h2><a class="toc-backref" href="#id15" role="doc-backlink">Setting Routing Rules</a><a class="headerlink" href="#setting-routing-rules" title="Permalink to this heading"></a></h2>
<p>Routing rules are defined in the <strong>app/Config/Routes.php</strong> file. In it youll see that
it creates an instance of the RouteCollection class (<code class="docutils literal notranslate"><span class="pre">$routes</span></code>) that permits you to specify your own routing criteria.
Routes can be specified using placeholders or Regular Expressions.</p>
@ -431,7 +431,7 @@ passed to it, then they would be listed after the method name, separated by forw
</pre></div>
</div>
<section id="examples">
<h3><a class="toc-backref" href="#id15" role="doc-backlink">Examples</a><a class="headerlink" href="#examples" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id16" role="doc-backlink">Examples</a><a class="headerlink" href="#examples" title="Permalink to this heading"></a></h3>
<p>Here are a few basic routing examples.</p>
<p>A URL containing the word <strong>journals</strong> in the first segment will be mapped to the <code class="docutils literal notranslate"><span class="pre">\App\Controllers\Blogs</span></code> class,
and the default method, which is usually <code class="docutils literal notranslate"><span class="pre">index()</span></code>:</p>
@ -463,7 +463,7 @@ and the <code class="docutils literal notranslate"><span class="pre">productLook
</div>
</section>
<section id="http-verb-routes">
<span id="routing-http-verb-routes"></span><h3><a class="toc-backref" href="#id16" role="doc-backlink">HTTP verb Routes</a><a class="headerlink" href="#http-verb-routes" title="Permalink to this heading"></a></h3>
<span id="routing-http-verb-routes"></span><h3><a class="toc-backref" href="#id17" role="doc-backlink">HTTP verb Routes</a><a class="headerlink" href="#http-verb-routes" title="Permalink to this heading"></a></h3>
<p>You can use any standard HTTP verb (GET, POST, PUT, DELETE, OPTIONS, etc):</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -480,9 +480,9 @@ and the <code class="docutils literal notranslate"><span class="pre">productLook
</div>
</section>
<section id="specifying-route-handlers">
<h3><a class="toc-backref" href="#id17" role="doc-backlink">Specifying Route Handlers</a><a class="headerlink" href="#specifying-route-handlers" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id18" role="doc-backlink">Specifying Route Handlers</a><a class="headerlink" href="#specifying-route-handlers" title="Permalink to this heading"></a></h3>
<section id="controller-s-namespace">
<span id="controllers-namespace"></span><h4><a class="toc-backref" href="#id18" role="doc-backlink">Controllers Namespace</a><a class="headerlink" href="#controller-s-namespace" title="Permalink to this heading"></a></h4>
<span id="controllers-namespace"></span><h4><a class="toc-backref" href="#id19" role="doc-backlink">Controllers Namespace</a><a class="headerlink" href="#controller-s-namespace" title="Permalink to this heading"></a></h4>
<p>When you specify a controller and method name as a string, if a controller is
written without a leading <code class="docutils literal notranslate"><span class="pre">\</span></code>, the <a class="reference internal" href="#routing-default-namespace"><span class="std std-ref">Default Namespace</span></a> will be
prepended:</p>
@ -509,7 +509,7 @@ prepended:</p>
<p>See <a class="reference internal" href="#assigning-namespace"><span class="std std-ref">Assigning Namespace</span></a> for details.</p>
</section>
<section id="array-callable-syntax">
<h4><a class="toc-backref" href="#id19" role="doc-backlink">Array Callable Syntax</a><a class="headerlink" href="#array-callable-syntax" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id20" role="doc-backlink">Array Callable Syntax</a><a class="headerlink" href="#array-callable-syntax" title="Permalink to this heading"></a></h4>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.2.0.</span></p>
</div>
@ -556,7 +556,7 @@ In such a case, you can specify the parameters manually:</p>
</section>
</section>
<section id="using-closures">
<h4><a class="toc-backref" href="#id20" role="doc-backlink">Using Closures</a><a class="headerlink" href="#using-closures" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id21" role="doc-backlink">Using Closures</a><a class="headerlink" href="#using-closures" title="Permalink to this heading"></a></h4>
<p>You can use an anonymous function, or Closure, as the destination that a route maps to. This function will be
executed when the user visits that URI. This is handy for quickly executing small tasks, or even just showing
a simple view:</p>
@ -574,9 +574,9 @@ a simple view:</p>
</section>
</section>
<section id="specifying-route-paths">
<h3><a class="toc-backref" href="#id21" role="doc-backlink">Specifying Route Paths</a><a class="headerlink" href="#specifying-route-paths" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id22" role="doc-backlink">Specifying Route Paths</a><a class="headerlink" href="#specifying-route-paths" title="Permalink to this heading"></a></h3>
<section id="placeholders">
<h4><a class="toc-backref" href="#id22" role="doc-backlink">Placeholders</a><a class="headerlink" href="#placeholders" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id23" role="doc-backlink">Placeholders</a><a class="headerlink" href="#placeholders" title="Permalink to this heading"></a></h4>
<p>A typical route might look something like this:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -685,7 +685,7 @@ routes. With the examples URLs from above:</p>
</section>
</section>
<section id="custom-placeholders">
<h4><a class="toc-backref" href="#id23" role="doc-backlink">Custom Placeholders</a><a class="headerlink" href="#custom-placeholders" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id24" role="doc-backlink">Custom Placeholders</a><a class="headerlink" href="#custom-placeholders" title="Permalink to this heading"></a></h4>
<p>You can create your own placeholders that can be used in your routes file to fully customize the experience
and readability.</p>
<p>You add new placeholders with the <code class="docutils literal notranslate"><span class="pre">addPlaceholder()</span></code> method. The first parameter is the string to be used as
@ -699,7 +699,7 @@ This must be called before you add the route:</p>
</div>
</section>
<section id="regular-expressions">
<h4><a class="toc-backref" href="#id24" role="doc-backlink">Regular Expressions</a><a class="headerlink" href="#regular-expressions" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id25" role="doc-backlink">Regular Expressions</a><a class="headerlink" href="#regular-expressions" title="Permalink to this heading"></a></h4>
<p>If you prefer you can use regular expressions to define your routing rules. Any valid regular expression
is allowed, as are back-references.</p>
<div class="admonition important">
@ -735,7 +735,7 @@ redirect them back to the same page after they log in, you may find this example
</section>
</section>
<section id="view-routes">
<span id="id1"></span><h3><a class="toc-backref" href="#id25" role="doc-backlink">View Routes</a><a class="headerlink" href="#view-routes" title="Permalink to this heading"></a></h3>
<span id="id1"></span><h3><a class="toc-backref" href="#id26" role="doc-backlink">View Routes</a><a class="headerlink" href="#view-routes" title="Permalink to this heading"></a></h3>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.3.0.</span></p>
</div>
@ -761,7 +761,7 @@ They are available as an array, indexed in the order they appear in the route.</
</div>
</section>
<section id="redirecting-routes">
<span id="id2"></span><h3><a class="toc-backref" href="#id26" role="doc-backlink">Redirecting Routes</a><a class="headerlink" href="#redirecting-routes" title="Permalink to this heading"></a></h3>
<span id="id2"></span><h3><a class="toc-backref" href="#id27" role="doc-backlink">Redirecting Routes</a><a class="headerlink" href="#redirecting-routes" title="Permalink to this heading"></a></h3>
<p>Any site that lives long enough is bound to have pages that move. You can specify routes that should redirect
to other routes with the <code class="docutils literal notranslate"><span class="pre">addRedirect()</span></code> method. The first parameter is the URI pattern for the old route. The
second parameter is either the new URI to redirect to, or the name of a named route. The third parameter is
@ -793,7 +793,7 @@ redirect and is recommended in most cases:</p>
controller can be loaded.</p>
</section>
<section id="environment-restrictions">
<h3><a class="toc-backref" href="#id27" role="doc-backlink">Environment Restrictions</a><a class="headerlink" href="#environment-restrictions" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id28" role="doc-backlink">Environment Restrictions</a><a class="headerlink" href="#environment-restrictions" title="Permalink to this heading"></a></h3>
<p>You can create a set of routes that will only be viewable in a certain environment. This allows you to create
tools that only the developer can use on their local machines that are not reachable on testing or production servers.
This can be done with the <code class="docutils literal notranslate"><span class="pre">environment()</span></code> method. The first parameter is the name of the environment. Any
@ -807,7 +807,7 @@ routes defined within this closure are only accessible from the given environmen
</div>
</section>
<section id="routes-with-any-http-verbs">
<h3><a class="toc-backref" href="#id28" role="doc-backlink">Routes with any HTTP verbs</a><a class="headerlink" href="#routes-with-any-http-verbs" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id29" role="doc-backlink">Routes with any HTTP verbs</a><a class="headerlink" href="#routes-with-any-http-verbs" title="Permalink to this heading"></a></h3>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>This method exists only for backward compatibility. Do not use it
@ -836,7 +836,7 @@ when trying to find a match.</p>
</div>
</section>
<section id="mapping-multiple-routes">
<h3><a class="toc-backref" href="#id29" role="doc-backlink">Mapping Multiple Routes</a><a class="headerlink" href="#mapping-multiple-routes" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id30" role="doc-backlink">Mapping Multiple Routes</a><a class="headerlink" href="#mapping-multiple-routes" title="Permalink to this heading"></a></h3>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>This method exists only for backward compatibility. Do not use it
@ -863,7 +863,7 @@ define an array of routes and then pass it as the first parameter to the <code c
</div>
</section>
<section id="command-line-only-routes">
<span id="id3"></span><h3><a class="toc-backref" href="#id30" role="doc-backlink">Command-Line Only Routes</a><a class="headerlink" href="#command-line-only-routes" title="Permalink to this heading"></a></h3>
<span id="id3"></span><h3><a class="toc-backref" href="#id31" role="doc-backlink">Command-Line Only Routes</a><a class="headerlink" href="#command-line-only-routes" title="Permalink to this heading"></a></h3>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>It is recommended to use Spark Commands for CLI scripts instead of calling controllers via CLI.
@ -887,7 +887,7 @@ anyone could access the command with the help of Auto Routing (Legacy) via HTTP.
</section>
</section>
<section id="global-options">
<h2><a class="toc-backref" href="#id31" role="doc-backlink">Global Options</a><a class="headerlink" href="#global-options" title="Permalink to this heading"></a></h2>
<h2><a class="toc-backref" href="#id32" role="doc-backlink">Global Options</a><a class="headerlink" href="#global-options" title="Permalink to this heading"></a></h2>
<p>All of the methods for creating a route (<code class="docutils literal notranslate"><span class="pre">get()</span></code>, <code class="docutils literal notranslate"><span class="pre">post()</span></code>, <a class="reference internal" href="restful.html"><span class="doc">resource()</span></a> etc) can take an array of options that
can modify the generated routes, or further restrict them. The <code class="docutils literal notranslate"><span class="pre">$options</span></code> array is always the last parameter:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -907,7 +907,7 @@ can modify the generated routes, or further restrict them. The <code class="docu
</pre></div>
</div>
<section id="applying-filters">
<span id="id4"></span><h3><a class="toc-backref" href="#id32" role="doc-backlink">Applying Filters</a><a class="headerlink" href="#applying-filters" title="Permalink to this heading"></a></h3>
<span id="id4"></span><h3><a class="toc-backref" href="#id33" role="doc-backlink">Applying Filters</a><a class="headerlink" href="#applying-filters" title="Permalink to this heading"></a></h3>
<p>You can alter the behavior of specific routes by supplying filters to run before or after the controller. This is especially handy during authentication or api logging.
The value for the filter can be a string or an array of strings:</p>
<ul class="simple">
@ -925,7 +925,7 @@ in which case the filter you specified to the route will not be applied.
See <a class="reference internal" href="#use-defined-routes-only"><span class="std std-ref">Use Defined Routes Only</span></a> to disable auto-routing.</p>
</div>
<section id="alias-filter">
<h4><a class="toc-backref" href="#id33" role="doc-backlink">Alias Filter</a><a class="headerlink" href="#alias-filter" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id34" role="doc-backlink">Alias Filter</a><a class="headerlink" href="#alias-filter" title="Permalink to this heading"></a></h4>
<p>You specify an alias defined in <strong>app/Config/Filters.php</strong> for the filter value:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -940,7 +940,7 @@ See <a class="reference internal" href="#use-defined-routes-only"><span class="s
</div>
</section>
<section id="classname-filter">
<h4><a class="toc-backref" href="#id34" role="doc-backlink">Classname Filter</a><a class="headerlink" href="#classname-filter" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id35" role="doc-backlink">Classname Filter</a><a class="headerlink" href="#classname-filter" title="Permalink to this heading"></a></h4>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.1.5.</span></p>
</div>
@ -952,7 +952,7 @@ See <a class="reference internal" href="#use-defined-routes-only"><span class="s
</div>
</section>
<section id="multiple-filters">
<h4><a class="toc-backref" href="#id35" role="doc-backlink">Multiple Filters</a><a class="headerlink" href="#multiple-filters" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id36" role="doc-backlink">Multiple Filters</a><a class="headerlink" href="#multiple-filters" title="Permalink to this heading"></a></h4>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.1.5.</span></p>
</div>
@ -980,7 +980,7 @@ to the filters <code class="docutils literal notranslate"><span class="pre">b
</section>
</section>
<section id="assigning-namespace">
<span id="id5"></span><h3><a class="toc-backref" href="#id36" role="doc-backlink">Assigning Namespace</a><a class="headerlink" href="#assigning-namespace" title="Permalink to this heading"></a></h3>
<span id="id5"></span><h3><a class="toc-backref" href="#id37" role="doc-backlink">Assigning Namespace</a><a class="headerlink" href="#assigning-namespace" title="Permalink to this heading"></a></h3>
<p>While a <a class="reference internal" href="#routing-default-namespace"><span class="std std-ref">Default Namespace</span></a> will be prepended to the generated controllers, you can also specify
a different namespace to be used in any options array, with the <code class="docutils literal notranslate"><span class="pre">namespace</span></code> option. The value should be the
namespace you want modified:</p>
@ -995,7 +995,7 @@ For any methods that create multiple routes, the new namespace is attached to al
or, in the case of <code class="docutils literal notranslate"><span class="pre">group()</span></code>, all routes generated while in the closure.</p>
</section>
<section id="limit-to-hostname">
<h3><a class="toc-backref" href="#id37" role="doc-backlink">Limit to Hostname</a><a class="headerlink" href="#limit-to-hostname" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id38" role="doc-backlink">Limit to Hostname</a><a class="headerlink" href="#limit-to-hostname" title="Permalink to this heading"></a></h3>
<p>You can restrict groups of routes to function only in certain domain or sub-domains of your application
by passing the “hostname” option along with the desired domain to allow it on as part of the options array:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -1007,7 +1007,7 @@ by passing the “hostname” option along with the desired domain to allow it o
It would not work under the main site at <strong>example.com</strong>.</p>
</section>
<section id="limit-to-subdomains">
<h3><a class="toc-backref" href="#id38" role="doc-backlink">Limit to Subdomains</a><a class="headerlink" href="#limit-to-subdomains" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id39" role="doc-backlink">Limit to Subdomains</a><a class="headerlink" href="#limit-to-subdomains" title="Permalink to this heading"></a></h3>
<p>When the <code class="docutils literal notranslate"><span class="pre">subdomain</span></code> option is present, the system will restrict the routes to only be available on that
sub-domain. The route will only be matched if the subdomain is the one the application is being viewed through:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -1032,7 +1032,7 @@ to separate suffixes or www) can potentially lead to false positives.</p>
</div>
</section>
<section id="offsetting-the-matched-parameters">
<h3><a class="toc-backref" href="#id39" role="doc-backlink">Offsetting the Matched Parameters</a><a class="headerlink" href="#offsetting-the-matched-parameters" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id40" role="doc-backlink">Offsetting the Matched Parameters</a><a class="headerlink" href="#offsetting-the-matched-parameters" title="Permalink to this heading"></a></h3>
<p>You can offset the matched parameters in your route by any numeric value with the <code class="docutils literal notranslate"><span class="pre">offset</span></code> option, with the
value being the number of segments to offset.</p>
<p>This can be beneficial when developing APIs with the first URI segment being the version number. It can also
@ -1048,7 +1048,7 @@ be used when the first parameter is a language string:</p>
</section>
</section>
<section id="reverse-routing">
<span id="id6"></span><h2><a class="toc-backref" href="#id40" role="doc-backlink">Reverse Routing</a><a class="headerlink" href="#reverse-routing" title="Permalink to this heading"></a></h2>
<span id="id6"></span><h2><a class="toc-backref" href="#id41" role="doc-backlink">Reverse Routing</a><a class="headerlink" href="#reverse-routing" title="Permalink to this heading"></a></h2>
<p>Reverse routing allows you to define the controller and method, as well as any parameters, that a link should go
to, and have the router lookup the current route to it. This allows route definitions to change without you having
to update your application code. This is typically used within views to create links.</p>
@ -1070,7 +1070,7 @@ should be passed to the route are passed in next:</p>
</div>
</section>
<section id="named-routes">
<span id="using-named-routes"></span><h2><a class="toc-backref" href="#id41" role="doc-backlink">Named Routes</a><a class="headerlink" href="#named-routes" title="Permalink to this heading"></a></h2>
<span id="using-named-routes"></span><h2><a class="toc-backref" href="#id42" role="doc-backlink">Named Routes</a><a class="headerlink" href="#named-routes" title="Permalink to this heading"></a></h2>
<p>You can name routes to make your application less fragile. This applies a name to a route that can be called
later, and even if the route definition changes, all of the links in your application built with <a class="reference internal" href="../helpers/url_helper.html#url_to" title="url_to"><code class="xref php php-func docutils literal notranslate"><span class="pre">url_to()</span></code></a>
will still work without you having to make any changes. A route is named by passing in the <code class="docutils literal notranslate"><span class="pre">as</span></code> option
@ -1090,7 +1090,7 @@ with the name of the route:</p>
<p>This has the added benefit of making the views more readable, too.</p>
</section>
<section id="grouping-routes">
<h2><a class="toc-backref" href="#id42" role="doc-backlink">Grouping Routes</a><a class="headerlink" href="#grouping-routes" title="Permalink to this heading"></a></h2>
<h2><a class="toc-backref" href="#id43" role="doc-backlink">Grouping Routes</a><a class="headerlink" href="#grouping-routes" title="Permalink to this heading"></a></h2>
<p>You can group your routes under a common name with the <code class="docutils literal notranslate"><span class="pre">group()</span></code> method. The group name becomes a segment that
appears prior to the routes defined inside of the group. This allows you to reduce the typing needed to build out an
extensive set of routes that all share the opening string, like when building an admin area:</p>
@ -1104,7 +1104,7 @@ extensive set of routes that all share the opening string, like when building an
</div>
<p>This would prefix the <strong>users</strong> and <strong>blog</strong> URIs with <strong>admin</strong>, handling URLs like <strong>admin/users</strong> and <strong>admin/blog</strong>.</p>
<section id="setting-namespace">
<h3><a class="toc-backref" href="#id43" role="doc-backlink">Setting Namespace</a><a class="headerlink" href="#setting-namespace" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id44" role="doc-backlink">Setting Namespace</a><a class="headerlink" href="#setting-namespace" title="Permalink to this heading"></a></h3>
<p>If you need to assign options to a group, like a <a class="reference internal" href="#assigning-namespace"><span class="std std-ref">Assigning Namespace</span></a>, do it before the callback:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -1116,7 +1116,7 @@ extensive set of routes that all share the opening string, like when building an
<p>This would handle a resource route to the <code class="docutils literal notranslate"><span class="pre">App\API\v1\Users</span></code> controller with the <strong>api/users</strong> URI.</p>
</section>
<section id="setting-filters">
<h3><a class="toc-backref" href="#id44" role="doc-backlink">Setting Filters</a><a class="headerlink" href="#setting-filters" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id45" role="doc-backlink">Setting Filters</a><a class="headerlink" href="#setting-filters" title="Permalink to this heading"></a></h3>
<p>You can also use a specific <a class="reference internal" href="filters.html"><span class="doc">filter</span></a> for a group of routes. This will always
run the filter before or after the controller. This is especially handy during authentication or api logging:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -1129,7 +1129,7 @@ run the filter before or after the controller. This is especially handy during a
<p>The value for the filter must match one of the aliases defined within <strong>app/Config/Filters.php</strong>.</p>
</section>
<section id="setting-other-options">
<h3><a class="toc-backref" href="#id45" role="doc-backlink">Setting Other Options</a><a class="headerlink" href="#setting-other-options" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id46" role="doc-backlink">Setting Other Options</a><a class="headerlink" href="#setting-other-options" title="Permalink to this heading"></a></h3>
<p>At some point, you may want to group routes for the purpose of applying filters or other route
config options like namespace, subdomain, etc. Without necessarily needing to add a prefix to the group, you can pass
an empty string in place of the prefix and the routes in the group will be routed as though the group never existed but with the
@ -1145,7 +1145,7 @@ given route config options:</p>
</div>
</section>
<section id="nesting-groups">
<h3><a class="toc-backref" href="#id46" role="doc-backlink">Nesting Groups</a><a class="headerlink" href="#nesting-groups" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id47" role="doc-backlink">Nesting Groups</a><a class="headerlink" href="#nesting-groups" title="Permalink to this heading"></a></h3>
<p>It is possible to nest groups within groups for finer organization if you need it:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -1164,7 +1164,7 @@ given route config options:</p>
</section>
</section>
<section id="route-priority">
<span id="routing-priority"></span><h2><a class="toc-backref" href="#id47" role="doc-backlink">Route Priority</a><a class="headerlink" href="#route-priority" title="Permalink to this heading"></a></h2>
<span id="routing-priority"></span><h2><a class="toc-backref" href="#id48" role="doc-backlink">Route Priority</a><a class="headerlink" href="#route-priority" title="Permalink to this heading"></a></h2>
<p>Routes are registered in the routing table in the order in which they are defined. This means that when a URI is accessed, the first matching route will be executed.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
@ -1172,7 +1172,7 @@ given route config options:</p>
</div>
<p>You can check registered routes in the routing table by running the <a class="reference internal" href="#routing-spark-routes"><span class="std std-ref">spark routes</span></a> command.</p>
<section id="changing-route-priority">
<h3><a class="toc-backref" href="#id48" role="doc-backlink">Changing Route Priority</a><a class="headerlink" href="#changing-route-priority" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id49" role="doc-backlink">Changing Route Priority</a><a class="headerlink" href="#changing-route-priority" title="Permalink to this heading"></a></h3>
<p>When working with modules, it can be a problem if the routes in the application contain wildcards.
Then the module routes will not be processed correctly.
You can solve this problem by lowering the priority of route processing using the <code class="docutils literal notranslate"><span class="pre">priority</span></code> option. The parameter
@ -1206,7 +1206,7 @@ Negative integers will be cast to the absolute value.</p>
</section>
</section>
<section id="routes-configuration-options">
<span id="id7"></span><h2><a class="toc-backref" href="#id49" role="doc-backlink">Routes Configuration Options</a><a class="headerlink" href="#routes-configuration-options" title="Permalink to this heading"></a></h2>
<span id="id7"></span><h2><a class="toc-backref" href="#id50" role="doc-backlink">Routes Configuration Options</a><a class="headerlink" href="#routes-configuration-options" title="Permalink to this heading"></a></h2>
<p>The RoutesCollection class provides several options that affect all routes, and can be modified to meet your
applications needs. These options are available in <strong>app/Config/Routing.php</strong>.</p>
<div class="admonition note">
@ -1216,7 +1216,7 @@ In previous versions, the setter methods were used in <strong>app/Config/Routes.
to change settings.</p>
</div>
<section id="default-namespace">
<span id="routing-default-namespace"></span><h3><a class="toc-backref" href="#id50" role="doc-backlink">Default Namespace</a><a class="headerlink" href="#default-namespace" title="Permalink to this heading"></a></h3>
<span id="routing-default-namespace"></span><h3><a class="toc-backref" href="#id51" role="doc-backlink">Default Namespace</a><a class="headerlink" href="#default-namespace" title="Permalink to this heading"></a></h3>
<p>When matching a controller to a route, the router will add the default namespace value to the front of the controller
specified by the route. By default, this value is <code class="docutils literal notranslate"><span class="pre">App\Controllers</span></code>.</p>
<p>If you set the value empty string (<code class="docutils literal notranslate"><span class="pre">''</span></code>), it leaves each route to specify the fully namespaced
@ -1258,7 +1258,7 @@ then you can change this value to save typing:</p>
</div>
</section>
<section id="translate-uri-dashes">
<h3><a class="toc-backref" href="#id51" role="doc-backlink">Translate URI Dashes</a><a class="headerlink" href="#translate-uri-dashes" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id52" role="doc-backlink">Translate URI Dashes</a><a class="headerlink" href="#translate-uri-dashes" title="Permalink to this heading"></a></h3>
<p>This option enables you to automatically replace dashes (<code class="docutils literal notranslate"><span class="pre">-</span></code>) with underscores in the controller and method
URI segments when used in Auto Routing, thus saving you additional route entries if you need to do that. This is required because the dash isnt a valid class or method name character and would cause a fatal error if you try to use it:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -1288,7 +1288,7 @@ underscores (<strong>foo_bar</strong>) is not accessible.</p>
</div>
</section>
<section id="use-defined-routes-only">
<span id="id8"></span><h3><a class="toc-backref" href="#id52" role="doc-backlink">Use Defined Routes Only</a><a class="headerlink" href="#use-defined-routes-only" title="Permalink to this heading"></a></h3>
<span id="id8"></span><h3><a class="toc-backref" href="#id53" role="doc-backlink">Use Defined Routes Only</a><a class="headerlink" href="#use-defined-routes-only" title="Permalink to this heading"></a></h3>
<p>Since v4.2.0, the auto-routing is disabled by default.</p>
<p>When no defined route is found that matches the URI, the system will attempt to match that URI against the
controllers and methods when Auto Routing is enabled.</p>
@ -1318,7 +1318,7 @@ requests. If the URI is accessible by the GET method, the CSRF protection will n
</div>
</section>
<section id="override">
<h3><a class="toc-backref" href="#id53" role="doc-backlink">404 Override</a><a class="headerlink" href="#override" title="Permalink to this heading"></a></h3>
<span id="id9"></span><h3><a class="toc-backref" href="#id54" role="doc-backlink">404 Override</a><a class="headerlink" href="#override" title="Permalink to this heading"></a></h3>
<p>When a page is not found that matches the current URI, the system will show a
generic 404 view. Using the <code class="docutils literal notranslate"><span class="pre">$override404</span></code> property within the routing config
file, you can define controller class/method for 404 routes.</p>
@ -1363,7 +1363,7 @@ information on how to set the status code.</p>
</div>
</section>
<section id="route-processing-by-priority">
<h3><a class="toc-backref" href="#id54" role="doc-backlink">Route Processing by Priority</a><a class="headerlink" href="#route-processing-by-priority" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id55" role="doc-backlink">Route Processing by Priority</a><a class="headerlink" href="#route-processing-by-priority" title="Permalink to this heading"></a></h3>
<p>Enables or disables processing of the routes queue by priority. Lowering the priority is defined in the route option.
Disabled by default. This functionality affects all routes.
For an example use of lowering the priority see <a class="reference internal" href="#routing-priority"><span class="std std-ref">Route Priority</span></a>:</p>
@ -1391,7 +1391,7 @@ For an example use of lowering the priority see <a class="reference internal" hr
</section>
</section>
<section id="auto-routing-improved">
<span id="id9"></span><h2><a class="toc-backref" href="#id55" role="doc-backlink">Auto Routing (Improved)</a><a class="headerlink" href="#auto-routing-improved" title="Permalink to this heading"></a></h2>
<span id="id10"></span><h2><a class="toc-backref" href="#id56" role="doc-backlink">Auto Routing (Improved)</a><a class="headerlink" href="#auto-routing-improved" title="Permalink to this heading"></a></h2>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.2.0.</span></p>
</div>
@ -1414,7 +1414,7 @@ and execute the corresponding controller methods.</p>
<p>Auto Routing (Improved) is disabled by default. To use it, see below.</p>
</div>
<section id="enable-auto-routing">
<span id="enabled-auto-routing-improved"></span><h3><a class="toc-backref" href="#id56" role="doc-backlink">Enable Auto Routing</a><a class="headerlink" href="#enable-auto-routing" title="Permalink to this heading"></a></h3>
<span id="enabled-auto-routing-improved"></span><h3><a class="toc-backref" href="#id57" role="doc-backlink">Enable Auto Routing</a><a class="headerlink" href="#enable-auto-routing" title="Permalink to this heading"></a></h3>
<p>To use it, you need to change the setting <code class="docutils literal notranslate"><span class="pre">$autoRoute</span></code> option to <code class="docutils literal notranslate"><span class="pre">true</span></code> in <strong>app/Config/Routing.php</strong>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nx">bool</span> <span class="nv">$autoRoute</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span>
</pre></div>
@ -1425,7 +1425,7 @@ and execute the corresponding controller methods.</p>
</div>
</section>
<section id="uri-segments">
<h3><a class="toc-backref" href="#id57" role="doc-backlink">URI Segments</a><a class="headerlink" href="#uri-segments" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id58" role="doc-backlink">URI Segments</a><a class="headerlink" href="#uri-segments" title="Permalink to this heading"></a></h3>
<p>The segments in the URL, in following with the Model-View-Controller approach, usually represent:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">class</span><span class="o">/</span><span class="nx">method</span><span class="o">/</span><span class="nx">ID</span>
</pre></div>
@ -1449,10 +1449,10 @@ and executes <code class="docutils literal notranslate"><span class="pre">getHel
<p>See <a class="reference internal" href="controllers.html#controller-auto-routing-improved"><span class="std std-ref">Auto Routing in Controllers</span></a> for more info.</p>
</section>
<section id="configuration-options">
<span id="routing-auto-routing-improved-configuration-options"></span><h3><a class="toc-backref" href="#id58" role="doc-backlink">Configuration Options</a><a class="headerlink" href="#configuration-options" title="Permalink to this heading"></a></h3>
<span id="routing-auto-routing-improved-configuration-options"></span><h3><a class="toc-backref" href="#id59" role="doc-backlink">Configuration Options</a><a class="headerlink" href="#configuration-options" title="Permalink to this heading"></a></h3>
<p>These options are available in the <strong>app/Config/Routing.php</strong> file.</p>
<section id="default-controller">
<h4><a class="toc-backref" href="#id59" role="doc-backlink">Default Controller</a><a class="headerlink" href="#default-controller" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id60" role="doc-backlink">Default Controller</a><a class="headerlink" href="#default-controller" title="Permalink to this heading"></a></h4>
<section id="for-site-root-uri">
<h5>For Site Root URI<a class="headerlink" href="#for-site-root-uri" title="Permalink to this heading"></a></h5>
<p>When a user visits the root of your site (i.e., <strong>example.com</strong>) the controller
@ -1478,7 +1478,7 @@ When the default controller is <code class="docutils literal notranslate"><span
</section>
</section>
<section id="default-method">
<h4><a class="toc-backref" href="#id60" role="doc-backlink">Default Method</a><a class="headerlink" href="#default-method" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id61" role="doc-backlink">Default Method</a><a class="headerlink" href="#default-method" title="Permalink to this heading"></a></h4>
<p>This works similar to the default controller setting, but is used to determine the default method that is used
when a controller is found that matches the URI, but no segment exists for the method. The default value is
<code class="docutils literal notranslate"><span class="pre">index</span></code>.</p>
@ -1495,7 +1495,7 @@ In the example above, you can access <strong>example.com/products</strong>, but
</section>
</section>
<section id="module-routing">
<span id="auto-routing-improved-module-routing"></span><h3><a class="toc-backref" href="#id61" role="doc-backlink">Module Routing</a><a class="headerlink" href="#module-routing" title="Permalink to this heading"></a></h3>
<span id="auto-routing-improved-module-routing"></span><h3><a class="toc-backref" href="#id62" role="doc-backlink">Module Routing</a><a class="headerlink" href="#module-routing" title="Permalink to this heading"></a></h3>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.4.0.</span></p>
</div>
@ -1521,7 +1521,7 @@ controller <code class="docutils literal notranslate"><span class="pre">Acme\Blo
</section>
</section>
<section id="auto-routing-legacy">
<span id="id10"></span><h2><a class="toc-backref" href="#id62" role="doc-backlink">Auto Routing (Legacy)</a><a class="headerlink" href="#auto-routing-legacy" title="Permalink to this heading"></a></h2>
<span id="id11"></span><h2><a class="toc-backref" href="#id63" role="doc-backlink">Auto Routing (Legacy)</a><a class="headerlink" href="#auto-routing-legacy" title="Permalink to this heading"></a></h2>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>This feature exists only for backward compatibility. Do not use it
@ -1543,7 +1543,7 @@ or CSRF protection are bypassed.</p>
<p>Auto Routing (Legacy) routes a HTTP request with <strong>any</strong> HTTP method to a controller method.</p>
</div>
<section id="enable-auto-routing-legacy">
<h3><a class="toc-backref" href="#id63" role="doc-backlink">Enable Auto Routing (Legacy)</a><a class="headerlink" href="#enable-auto-routing-legacy" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id64" role="doc-backlink">Enable Auto Routing (Legacy)</a><a class="headerlink" href="#enable-auto-routing-legacy" title="Permalink to this heading"></a></h3>
<p>Since v4.2.0, the auto-routing is disabled by default.</p>
<p>To use it, you need to change the setting <code class="docutils literal notranslate"><span class="pre">$autoRoute</span></code> option to <code class="docutils literal notranslate"><span class="pre">true</span></code> in <strong>app/Config/Routing.php</strong>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nx">bool</span> <span class="nv">$autoRoute</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span>
@ -1555,7 +1555,7 @@ or CSRF protection are bypassed.</p>
</div>
</section>
<section id="uri-segments-legacy">
<h3><a class="toc-backref" href="#id64" role="doc-backlink">URI Segments (Legacy)</a><a class="headerlink" href="#uri-segments-legacy" title="Permalink to this heading"></a></h3>
<h3><a class="toc-backref" href="#id65" role="doc-backlink">URI Segments (Legacy)</a><a class="headerlink" href="#uri-segments-legacy" title="Permalink to this heading"></a></h3>
<p>The segments in the URL, in following with the Model-View-Controller approach, usually represent:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">class</span><span class="o">/</span><span class="nx">method</span><span class="o">/</span><span class="nx">ID</span>
</pre></div>
@ -1574,10 +1574,10 @@ and executes <code class="docutils literal notranslate"><span class="pre">index(
<p>See <a class="reference internal" href="controllers.html#controller-auto-routing-legacy"><span class="std std-ref">Auto Routing (Legacy) in Controllers</span></a> for more info.</p>
</section>
<section id="configuration-options-legacy">
<span id="routing-auto-routing-legacy-configuration-options"></span><h3><a class="toc-backref" href="#id65" role="doc-backlink">Configuration Options (Legacy)</a><a class="headerlink" href="#configuration-options-legacy" title="Permalink to this heading"></a></h3>
<span id="routing-auto-routing-legacy-configuration-options"></span><h3><a class="toc-backref" href="#id66" role="doc-backlink">Configuration Options (Legacy)</a><a class="headerlink" href="#configuration-options-legacy" title="Permalink to this heading"></a></h3>
<p>These options are available in the <strong>app/Config/Routing.php</strong> file.</p>
<section id="default-controller-legacy">
<h4><a class="toc-backref" href="#id66" role="doc-backlink">Default Controller (Legacy)</a><a class="headerlink" href="#default-controller-legacy" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id67" role="doc-backlink">Default Controller (Legacy)</a><a class="headerlink" href="#default-controller-legacy" title="Permalink to this heading"></a></h4>
<section id="for-site-root-uri-legacy">
<h5>For Site Root URI (Legacy)<a class="headerlink" href="#for-site-root-uri-legacy" title="Permalink to this heading"></a></h5>
<p>When a user visits the root of your site (i.e., <strong>example.com</strong>) the controller
@ -1598,7 +1598,7 @@ in the controllers directory. For example, if the user visits <strong>example.co
</section>
</section>
<section id="default-method-legacy">
<h4><a class="toc-backref" href="#id67" role="doc-backlink">Default Method (Legacy)</a><a class="headerlink" href="#default-method-legacy" title="Permalink to this heading"></a></h4>
<h4><a class="toc-backref" href="#id68" role="doc-backlink">Default Method (Legacy)</a><a class="headerlink" href="#default-method-legacy" title="Permalink to this heading"></a></h4>
<p>This works similar to the default controller setting, but is used to determine the default method that is used
when a controller is found that matches the URI, but no segment exists for the method. The default value is
<code class="docutils literal notranslate"><span class="pre">index</span></code>.</p>
@ -1611,10 +1611,10 @@ controller existed, the <code class="docutils literal notranslate"><span class="
</section>
</section>
<section id="confirming-routes">
<h2><a class="toc-backref" href="#id68" role="doc-backlink">Confirming Routes</a><a class="headerlink" href="#confirming-routes" title="Permalink to this heading"></a></h2>
<h2><a class="toc-backref" href="#id69" role="doc-backlink">Confirming Routes</a><a class="headerlink" href="#confirming-routes" title="Permalink to this heading"></a></h2>
<p>CodeIgniter has the following <a class="reference internal" href="../cli/spark_commands.html"><span class="doc">command</span></a> to display all routes.</p>
<section id="spark-routes">
<span id="routing-spark-routes"></span><h3><a class="toc-backref" href="#id69" role="doc-backlink">spark routes</a><a class="headerlink" href="#spark-routes" title="Permalink to this heading"></a></h3>
<span id="routing-spark-routes"></span><h3><a class="toc-backref" href="#id70" role="doc-backlink">spark routes</a><a class="headerlink" href="#spark-routes" title="Permalink to this heading"></a></h3>
<p>Displays all routes and filters:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">php spark routes</span>
</pre></div>
@ -1640,8 +1640,8 @@ for the filters in <strong>app/Config/Filters.php</strong>), or it is displayed
<p>The <a class="reference internal" href="filters.html#spark-filter-check"><span class="std std-ref">spark filter:check</span></a> command can be used to check
for 100% accurate filters.</p>
</div>
<section id="id11">
<h4><a class="toc-backref" href="#id70" role="doc-backlink">Auto Routing (Improved)</a><a class="headerlink" href="#id11" title="Permalink to this heading"></a></h4>
<section id="id12">
<h4><a class="toc-backref" href="#id71" role="doc-backlink">Auto Routing (Improved)</a><a class="headerlink" href="#id12" title="Permalink to this heading"></a></h4>
<p>When you use Auto Routing (Improved), the output is like the following:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>+-----------+-------------------------+---------------+-----------------------------------+----------------+---------------+
| Method | Route | Name | Handler | Before Filters | After Filters |
@ -1675,8 +1675,8 @@ the <code class="docutils literal notranslate"><span class="pre">getFoo()</span>
due to a bug.</p>
</div>
</section>
<section id="id12">
<h4><a class="toc-backref" href="#id71" role="doc-backlink">Auto Routing (Legacy)</a><a class="headerlink" href="#id12" title="Permalink to this heading"></a></h4>
<section id="id13">
<h4><a class="toc-backref" href="#id72" role="doc-backlink">Auto Routing (Legacy)</a><a class="headerlink" href="#id13" title="Permalink to this heading"></a></h4>
<p>When you use Auto Routing (Legacy), the output is like the following:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>+--------+--------------------+---------------+-----------------------------------+----------------+---------------+
| Method | Route | Name | Handler | Before Filters | After Filters |
@ -1693,7 +1693,7 @@ due to a bug.</p>
</div>
</section>
<section id="sort-by-handler">
<span id="routing-spark-routes-sort-by-handler"></span><h4><a class="toc-backref" href="#id72" role="doc-backlink">Sort by Handler</a><a class="headerlink" href="#sort-by-handler" title="Permalink to this heading"></a></h4>
<span id="routing-spark-routes-sort-by-handler"></span><h4><a class="toc-backref" href="#id73" role="doc-backlink">Sort by Handler</a><a class="headerlink" href="#sort-by-handler" title="Permalink to this heading"></a></h4>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.3.0.</span></p>
</div>
@ -1703,7 +1703,7 @@ due to a bug.</p>
</div>
</section>
<section id="specify-host">
<span id="routing-spark-routes-specify-host"></span><h4><a class="toc-backref" href="#id73" role="doc-backlink">Specify Host</a><a class="headerlink" href="#specify-host" title="Permalink to this heading"></a></h4>
<span id="routing-spark-routes-specify-host"></span><h4><a class="toc-backref" href="#id74" role="doc-backlink">Specify Host</a><a class="headerlink" href="#specify-host" title="Permalink to this heading"></a></h4>
<div class="versionadded">
<p><span class="versionmodified added">New in version 4.4.0.</span></p>
</div>

Binary file not shown.

File diff suppressed because one or more lines are too long