diff --git a/user_guide_src/source/outgoing/views.rst b/user_guide_src/source/outgoing/views.rst index 3eb80427fc2a..70bbadcc7504 100644 --- a/user_guide_src/source/outgoing/views.rst +++ b/user_guide_src/source/outgoing/views.rst @@ -10,7 +10,9 @@ A view is simply a web page, or a page fragment, like a header, footer, sidebar, views can flexibly be embedded within other views (within other views, etc.) if you need this type of hierarchy. -Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework, +Views are never called directly, they must be loaded by a controller or :ref:`view route `. + +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 :doc:`Controllers ` page, you should do so before continuing. @@ -19,7 +21,7 @@ Using the example controller you created in the controller page, let's add a vie Creating a View =============== -Using your text editor, create a file called ``blog_view.php`` and put this in it:: +Using your text editor, create a file called **blog_view.php** and put this in it:: @@ -35,19 +37,27 @@ Then save the file in your **app/Views** directory. Displaying a View ================= -To load and display a particular view file you will use the following function: +To load and display a particular view file you will use the following code in your controller: .. literalinclude:: views/001.php + :lines: 2- Where *name* is the name of your view file. -.. important:: If the file extension is omitted, then the views are expected to end with the .php extension. +.. important:: If the file extension is omitted, then the views are expected to end with the **.php** extension. -Now, open the controller file you made earlier called ``Blog.php``, and replace the echo statement with the view function: +Now, create a file called **Blog.php** in the **app/Controllers** directory, +and put this in it: .. literalinclude:: views/002.php -If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this:: +Open the routing file located at **app/Config/Routes.php**, and look for the "Route Definitions". +Add the following code: + +.. literalinclude:: views/013.php + :lines: 2- + +If you visit your site, you should see your new view. The URL was similar to this:: example.com/index.php/blog/ @@ -69,6 +79,7 @@ Your view files can also be stored within sub-directories if you prefer that typ When doing so you will need to include the directory name loading the view. Example: .. literalinclude:: views/004.php + :lines: 2- Namespaced Views ================ @@ -77,8 +88,10 @@ You can store views under a **View** directory that is namespaced, and load that 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. -If you have ``example/blog`` directory that has a PSR-4 mapping set up in the :doc:`Autoloader ` living -under the namespace ``Example\Blog``, you could retrieve view files as if they were namespaced also. Following this +If you have **example/blog** directory that has a PSR-4 mapping set up in the :doc:`Autoloader ` living +under the namespace ``Example\Blog``, you could retrieve view files as if they were namespaced also. + +Following this example, you could load the **blog_view.php** file from **example/blog/Views** by prepending the namespace to the view name: .. literalinclude:: views/005.php @@ -88,15 +101,17 @@ example, you could load the **blog_view.php** file from **example/blog/Views** b Caching Views ============= -You can cache a view with the ``view`` command by passing a ``cache`` option with the number of seconds to cache +You can cache a view with the ``view()`` function by passing a ``cache`` option with the number of seconds to cache the view for, in the third parameter: .. literalinclude:: views/006.php + :lines: 2- By default, the view will be cached using the same name as the view file itself. You can customize this by passing along ``cache_name`` and the cache ID you wish to use: .. literalinclude:: views/007.php + :lines: 2- Adding Dynamic Data to the View =============================== @@ -105,6 +120,7 @@ Data is passed from the controller to the view by way of an array in the second Here's an example: .. literalinclude:: views/008.php + :lines: 2- Let's try it with your controller file. Open it and add this code: @@ -134,9 +150,10 @@ other views, potentially causing issues. If you would prefer to clean the data a into the ``$option`` array in the third parameter. .. literalinclude:: views/010.php + :lines: 2- Additionally, if you would like the default functionality of the ``view()`` function to be that it does clear the data -between calls, you can set ``$saveData`` to **false** in **app/Config/Views.php**. +between calls, you can set ``$saveData`` to ``false`` in **app/Config/Views.php**. Creating Loops ============== diff --git a/user_guide_src/source/outgoing/views/002.php b/user_guide_src/source/outgoing/views/002.php index dda8dc67f363..bb44fed77919 100644 --- a/user_guide_src/source/outgoing/views/002.php +++ b/user_guide_src/source/outgoing/views/002.php @@ -2,9 +2,7 @@ namespace App\Controllers; -use CodeIgniter\Controller; - -class Blog extends Controller +class Blog extends BaseController { public function index() { diff --git a/user_guide_src/source/outgoing/views/009.php b/user_guide_src/source/outgoing/views/009.php index 008d1cf85d26..3cb2e017aad9 100644 --- a/user_guide_src/source/outgoing/views/009.php +++ b/user_guide_src/source/outgoing/views/009.php @@ -2,9 +2,7 @@ namespace App\Controllers; -use CodeIgniter\Controller; - -class Blog extends Controller +class Blog extends BaseController { public function index() { diff --git a/user_guide_src/source/outgoing/views/011.php b/user_guide_src/source/outgoing/views/011.php index bb1a5fc21725..a74b670c3e49 100644 --- a/user_guide_src/source/outgoing/views/011.php +++ b/user_guide_src/source/outgoing/views/011.php @@ -2,9 +2,7 @@ namespace App\Controllers; -use CodeIgniter\Controller; - -class Blog extends Controller +class Blog extends BaseController { public function index() { diff --git a/user_guide_src/source/outgoing/views/012.php b/user_guide_src/source/outgoing/views/012.php index 5a3781fe6720..509b548a1813 100644 --- a/user_guide_src/source/outgoing/views/012.php +++ b/user_guide_src/source/outgoing/views/012.php @@ -5,7 +5,7 @@

-

My Todo List

+

My Todo List