Skip to content

Commit 5e762b4

Browse files
committed
docs: update views.rst content
1 parent 24e82dc commit 5e762b4

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

user_guide_src/source/outgoing/views.rst

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ A view is simply a web page, or a page fragment, like a header, footer, sidebar,
1010
views can flexibly be embedded within other views (within other views, etc.) if you need
1111
this type of hierarchy.
1212

13-
Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework,
13+
Views are never called directly, they must be loaded by a controller or :ref:`view route <view-routes>`.
14+
15+
Remember that in an MVC framework,
1416
the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have
1517
not read the :doc:`Controllers </incoming/controllers>` page, you should do so before continuing.
1618

@@ -35,19 +37,27 @@ Then save the file in your **app/Views** directory.
3537
Displaying a View
3638
=================
3739

38-
To load and display a particular view file you will use the following function:
40+
To load and display a particular view file you will use the following code in your controller:
3941

4042
.. literalinclude:: views/001.php
43+
:lines: 2-
4144

4245
Where *name* is the name of your view file.
4346

44-
.. important:: If the file extension is omitted, then the views are expected to end with the .php extension.
47+
.. important:: If the file extension is omitted, then the views are expected to end with the **.php** extension.
4548

46-
Now, open the controller file you made earlier called ``Blog.php``, and replace the echo statement with the view function:
49+
Now, create a file called **Blog.php** in the **app/Controllers** directory,
50+
and put this in it:
4751

4852
.. literalinclude:: views/002.php
4953

50-
If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this::
54+
Open the routing file located at **app/Config/Routes.php**, and look for the "Route Definitions".
55+
Add the following code:
56+
57+
.. literalinclude:: views/013.php
58+
:lines: 2-
59+
60+
If you visit your site, you should see your new view. The URL was similar to this::
5161

5262
example.com/index.php/blog/
5363

@@ -69,6 +79,7 @@ Your view files can also be stored within sub-directories if you prefer that typ
6979
When doing so you will need to include the directory name loading the view. Example:
7080

7181
.. literalinclude:: views/004.php
82+
:lines: 2-
7283

7384
Namespaced Views
7485
================
@@ -77,8 +88,10 @@ You can store views under a **View** directory that is namespaced, and load that
7788
PHP does not support loading non-class files from a namespace, CodeIgniter provides this feature to make it possible
7889
to package your views together in a module-like fashion for easy re-use or distribution.
7990

80-
If you have ``example/blog`` directory that has a PSR-4 mapping set up in the :doc:`Autoloader </concepts/autoloader>` living
81-
under the namespace ``Example\Blog``, you could retrieve view files as if they were namespaced also. Following this
91+
If you have **example/blog** directory that has a PSR-4 mapping set up in the :doc:`Autoloader </concepts/autoloader>` living
92+
under the namespace ``Example\Blog``, you could retrieve view files as if they were namespaced also.
93+
94+
Following this
8295
example, you could load the **blog_view.php** file from **example/blog/Views** by prepending the namespace to the view name:
8396

8497
.. literalinclude:: views/005.php
@@ -88,15 +101,17 @@ example, you could load the **blog_view.php** file from **example/blog/Views** b
88101
Caching Views
89102
=============
90103

91-
You can cache a view with the ``view`` command by passing a ``cache`` option with the number of seconds to cache
104+
You can cache a view with the ``view()`` function by passing a ``cache`` option with the number of seconds to cache
92105
the view for, in the third parameter:
93106

94107
.. literalinclude:: views/006.php
108+
:lines: 2-
95109

96110
By default, the view will be cached using the same name as the view file itself. You can customize this by passing
97111
along ``cache_name`` and the cache ID you wish to use:
98112

99113
.. literalinclude:: views/007.php
114+
:lines: 2-
100115

101116
Adding Dynamic Data to the View
102117
===============================
@@ -105,6 +120,7 @@ Data is passed from the controller to the view by way of an array in the second
105120
Here's an example:
106121

107122
.. literalinclude:: views/008.php
123+
:lines: 2-
108124

109125
Let's try it with your controller file. Open it and add this code:
110126

@@ -134,6 +150,7 @@ other views, potentially causing issues. If you would prefer to clean the data a
134150
into the ``$option`` array in the third parameter.
135151

136152
.. literalinclude:: views/010.php
153+
:lines: 2-
137154

138155
Additionally, if you would like the default functionality of the ``view()`` function to be that it does clear the data
139156
between calls, you can set ``$saveData`` to ``false`` in **app/Config/Views.php**.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use App\Controllers\Blog;
4+
5+
$routes->get('blog', [Blog::class, 'index']);

0 commit comments

Comments
 (0)