Update User Guide

This commit is contained in:
kenjis 2023-01-22 01:01:23 +00:00
parent b47bc466be
commit ea7098705e
2 changed files with 31 additions and 39 deletions

View File

@ -294,13 +294,14 @@
<p>A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact,
views can flexibly be embedded within other views (within other views, etc.) if you need
this type of hierarchy.</p>
<p>Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework,
<p>Views are never called directly, they must be loaded by a controller or <a class="reference internal" href="../incoming/routing.html#view-routes"><span class="std std-ref">view route</span></a>.</p>
<p>Remember that in an MVC framework,
the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have
not read the <a class="reference internal" href="../incoming/controllers.html"><span class="doc">Controllers</span></a> page, you should do so before continuing.</p>
<p>Using the example controller you created in the controller page, lets add a view to it.</p>
<section id="creating-a-view">
<h2><a class="toc-backref" href="#id2">Creating a View</a><a class="headerlink" href="#creating-a-view" title="Permalink to this headline"></a></h2>
<p>Using your text editor, create a file called <code class="docutils literal notranslate"><span class="pre">blog_view.php</span></code> and put this in it:</p>
<p>Using your text editor, create a file called <strong>blog_view.php</strong> and put this in it:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="nx">html</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="nx">head</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="nx">title</span><span class="o">&gt;</span><span class="nx">My</span> <span class="nx">Blog</span><span class="o">&lt;/</span><span class="nx">title</span><span class="o">&gt;</span>
@ -315,25 +316,22 @@ not read the <a class="reference internal" href="../incoming/controllers.html"><
</section>
<section id="displaying-a-view">
<h2><a class="toc-backref" href="#id3">Displaying a View</a><a class="headerlink" href="#displaying-a-view" title="Permalink to this headline"></a></h2>
<p>To load and display a particular view file you will use the following function:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">return</span> <span class="nx">view</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">);</span>
<p>To load and display a particular view file you will use the following code in your controller:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nx">view</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>Where <em>name</em> is the name of your view file.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>If the file extension is omitted, then the views are expected to end with the .php extension.</p>
<p>If the file extension is omitted, then the views are expected to end with the <strong>.php</strong> extension.</p>
</div>
<p>Now, open the controller file you made earlier called <code class="docutils literal notranslate"><span class="pre">Blog.php</span></code>, and replace the echo statement with the view function:</p>
<p>Now, create a file called <strong>Blog.php</strong> in the <strong>app/Controllers</strong> directory,
and put this in it:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">CodeIgniter\Controller</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Blog</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="k">class</span> <span class="nc">Blog</span> <span class="k">extends</span> <span class="nx">BaseController</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
<span class="p">{</span>
@ -342,7 +340,14 @@ not read the <a class="reference internal" href="../incoming/controllers.html"><
<span class="p">}</span>
</pre></div>
</div>
<p>If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this:</p>
<p>Open the routing file located at <strong>app/Config/Routes.php</strong>, and look for the “Route Definitions”.
Add the following code:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">use</span> <span class="nx">App\Controllers\Blog</span><span class="p">;</span>
<span class="nv">$routes</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;blog&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nx">Blog</span><span class="o">::</span><span class="na">class</span><span class="p">,</span> <span class="s1">&#39;index&#39;</span><span class="p">]);</span>
</pre></div>
</div>
<p>If you visit your site, you should see your new view. The URL was similar to this:</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">index</span><span class="o">.</span><span class="nx">php</span><span class="o">/</span><span class="nx">blog</span><span class="o">/</span>
</pre></div>
</div>
@ -380,9 +385,7 @@ content view, and a footer view. That might look something like this:</p>
<h2><a class="toc-backref" href="#id5">Storing Views within Sub-directories</a><a class="headerlink" href="#storing-views-within-sub-directories" title="Permalink to this headline"></a></h2>
<p>Your view files can also be stored within sub-directories if you prefer that type of organization.
When doing so you will need to include the directory name loading the view. Example:</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">return</span> <span class="nx">view</span><span class="p">(</span><span class="s1">&#39;directory_name/file_name&#39;</span><span class="p">);</span>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="nx">view</span><span class="p">(</span><span class="s1">&#39;directory_name/file_name&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
@ -391,8 +394,9 @@ When doing so you will need to include the directory name loading the view. Exam
<p>You can store views under a <strong>View</strong> directory that is namespaced, and load that view as if it was namespaced. While
PHP does not support loading non-class files from a namespace, CodeIgniter provides this feature to make it possible
to package your views together in a module-like fashion for easy re-use or distribution.</p>
<p>If you have <code class="docutils literal notranslate"><span class="pre">example/blog</span></code> directory that has a PSR-4 mapping set up in the <a class="reference internal" href="../concepts/autoloader.html"><span class="doc">Autoloader</span></a> living
under the namespace <code class="docutils literal notranslate"><span class="pre">Example\Blog</span></code>, you could retrieve view files as if they were namespaced also. Following this
<p>If you have <strong>example/blog</strong> directory that has a PSR-4 mapping set up in the <a class="reference internal" href="../concepts/autoloader.html"><span class="doc">Autoloader</span></a> living
under the namespace <code class="docutils literal notranslate"><span class="pre">Example\Blog</span></code>, you could retrieve view files as if they were namespaced also.</p>
<p>Following this
example, you could load the <strong>blog_view.php</strong> file from <strong>example/blog/Views</strong> by prepending the namespace to the view name:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
@ -402,19 +406,15 @@ example, you could load the <strong>blog_view.php</strong> file from <strong>exa
</section>
<section id="caching-views">
<span id="id1"></span><h2><a class="toc-backref" href="#id7">Caching Views</a><a class="headerlink" href="#caching-views" title="Permalink to this headline"></a></h2>
<p>You can cache a view with the <code class="docutils literal notranslate"><span class="pre">view</span></code> command by passing a <code class="docutils literal notranslate"><span class="pre">cache</span></code> option with the number of seconds to cache
<p>You can cache a view with the <code class="docutils literal notranslate"><span class="pre">view()</span></code> function by passing a <code class="docutils literal notranslate"><span class="pre">cache</span></code> option with the number of seconds to cache
the view for, in the third parameter:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Cache the view for 60 seconds</span>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Cache the view for 60 seconds</span>
<span class="k">return</span> <span class="nx">view</span><span class="p">(</span><span class="s1">&#39;file_name&#39;</span><span class="p">,</span> <span class="nv">$data</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;cache&#39;</span> <span class="o">=&gt;</span> <span class="mi">60</span><span class="p">]);</span>
</pre></div>
</div>
<p>By default, the view will be cached using the same name as the view file itself. You can customize this by passing
along <code class="docutils literal notranslate"><span class="pre">cache_name</span></code> and the cache ID you wish to use:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Cache the view for 60 seconds</span>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Cache the view for 60 seconds</span>
<span class="k">return</span> <span class="nx">view</span><span class="p">(</span><span class="s1">&#39;file_name&#39;</span><span class="p">,</span> <span class="nv">$data</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;cache&#39;</span> <span class="o">=&gt;</span> <span class="mi">60</span><span class="p">,</span> <span class="s1">&#39;cache_name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;my_cached_view&#39;</span><span class="p">]);</span>
</pre></div>
</div>
@ -423,9 +423,7 @@ along <code class="docutils literal notranslate"><span class="pre">cache_name</s
<h2><a class="toc-backref" href="#id8">Adding Dynamic Data to the View</a><a class="headerlink" href="#adding-dynamic-data-to-the-view" title="Permalink to this headline"></a></h2>
<p>Data is passed from the controller to the view by way of an array in the second parameter of the <code class="docutils literal notranslate"><span class="pre">view()</span></code> function.
Heres an example:</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">$data</span> <span class="o">=</span> <span class="p">[</span>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$data</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My title&#39;</span><span class="p">,</span>
<span class="s1">&#39;heading&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My Heading&#39;</span><span class="p">,</span>
<span class="s1">&#39;message&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My Message&#39;</span><span class="p">,</span>
@ -439,9 +437,7 @@ Heres an example:</p>
<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">CodeIgniter\Controller</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Blog</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="k">class</span> <span class="nc">Blog</span> <span class="k">extends</span> <span class="nx">BaseController</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
<span class="p">{</span>
@ -472,9 +468,7 @@ in a single request, you will not have to pass the desired data to each <code cl
<p>But this might not keep any data from “bleeding” into
other views, potentially causing issues. If you would prefer to clean the data after one call, you can pass the <code class="docutils literal notranslate"><span class="pre">saveData</span></code> option
into the <code class="docutils literal notranslate"><span class="pre">$option</span></code> array in the third parameter.</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">$data</span> <span class="o">=</span> <span class="p">[</span>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$data</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My title&#39;</span><span class="p">,</span>
<span class="s1">&#39;heading&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My Heading&#39;</span><span class="p">,</span>
<span class="s1">&#39;message&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My Message&#39;</span><span class="p">,</span>
@ -484,7 +478,7 @@ into the <code class="docutils literal notranslate"><span class="pre">$option</s
</pre></div>
</div>
<p>Additionally, if you would like the default functionality of the <code class="docutils literal notranslate"><span class="pre">view()</span></code> function to be that it does clear the data
between calls, you can set <code class="docutils literal notranslate"><span class="pre">$saveData</span></code> to <strong>false</strong> in <strong>app/Config/Views.php</strong>.</p>
between calls, you can set <code class="docutils literal notranslate"><span class="pre">$saveData</span></code> to <code class="docutils literal notranslate"><span class="pre">false</span></code> in <strong>app/Config/Views.php</strong>.</p>
</section>
</section>
<section id="creating-loops">
@ -497,9 +491,7 @@ typically be in the form of a multi-dimensional array.</p>
<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">CodeIgniter\Controller</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Blog</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="k">class</span> <span class="nc">Blog</span> <span class="k">extends</span> <span class="nx">BaseController</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
<span class="p">{</span>
@ -522,7 +514,7 @@ typically be in the form of a multi-dimensional array.</p>
<span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="cp">&lt;?</span><span class="o">=</span> <span class="nx">esc</span><span class="p">(</span><span class="nv">$heading</span><span class="p">)</span> <span class="cp">?&gt;</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">h3</span><span class="p">&gt;</span>My Todo List<span class="p">&lt;/</span><span class="nt">h3</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">h2</span><span class="p">&gt;</span>My Todo List<span class="p">&lt;/</span><span class="nt">h2</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="cp">&lt;?php</span> <span class="k">foreach</span> <span class="p">(</span><span class="nv">$todo_list</span> <span class="k">as</span> <span class="nv">$item</span><span class="p">)</span><span class="o">:</span> <span class="cp">?&gt;</span>

File diff suppressed because one or more lines are too long