Skip to content

Commit

Permalink
Merge pull request #8022 from kenjis/docs-improve-tutorial
Browse files Browse the repository at this point in the history
docs: improve tutorial
  • Loading branch information
kenjis authored Oct 12, 2023
2 parents 155859b + 334bf50 commit da6492d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 67 deletions.
34 changes: 17 additions & 17 deletions user_guide_src/source/tutorial/create_news_items.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ You can read more about the CSRF protection in :doc:`Security <../libraries/secu
because :ref:`auto-routing-legacy` permits any HTTP method to access a controller.
Accessing the controller with a method you don't expect could bypass the filter.

Adding Routing Rules
********************

Before you can start adding news items into your CodeIgniter application
you have to add an extra rule to **app/Config/Routes.php** file. Make sure your
file contains the following:

.. literalinclude:: create_news_items/004.php

The route directive for ``'news/new'`` is placed before the directive for ``'news/(:segment)'`` to ensure that the form to create a news item is displayed.

The ``$routes->post()`` line defines the router for a POST request. It matches
only a POST request to the URI path **/news**, and it maps to the ``create()`` method of
the ``News`` class.

You can read more about different routing types in :ref:`defined-route-routing`.

Create a Form
*************

Expand Down Expand Up @@ -151,23 +168,6 @@ never need to do that, since it is an auto-incrementing field in the database.
This helps protect against Mass Assignment Vulnerabilities. If your model is
handling your timestamps, you would also leave those out.

Adding Routing Rules
********************

Before you can start adding news items into your CodeIgniter application
you have to add an extra rule to **app/Config/Routes.php** file. Make sure your
file contains the following:

.. literalinclude:: create_news_items/004.php

The route directive for ``'news/new'`` is placed before the directive for ``'news/(:segment)'`` to ensure that the form to create a news item is displayed.

The ``$routes->post()`` line defines the router for a POST request. It matches
only a POST request to the URI path **/news**, and it maps to the ``create()`` method of
the ``News`` class.

You can read more about different routing types in :ref:`defined-route-routing`.

Create a News Item
******************

Expand Down
22 changes: 11 additions & 11 deletions user_guide_src/source/tutorial/news_section.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ that are going to display the news items to the user. This could be done
in our ``Pages`` controller created earlier, but for the sake of clarity,
a new ``News`` controller is defined.

Adding Routing Rules
====================

Modify your **app/Config/Routes.php** file, so it looks as follows:

.. literalinclude:: news_section/008.php

This makes sure the requests reach the ``News`` controller instead of
going directly to the ``Pages`` controller. The second ``$routes->get()`` line
routes URI's with a slug to the ``show()`` method in the ``News`` controller.

Create News Controller
======================

Expand Down Expand Up @@ -200,17 +211,6 @@ The only thing left to do is create the corresponding view at

.. literalinclude:: news_section/007.php

Adding Routing Rules
********************

Modify your **app/Config/Routes.php** file, so it looks as follows:

.. literalinclude:: news_section/008.php

This makes sure the requests reach the ``News`` controller instead of
going directly to the ``Pages`` controller. The second ``$routes->get()`` line
routes URI's with a slug to the ``show()`` method in the ``News`` controller.

Point your browser to your "news" page, i.e., **localhost:8080/news**,
you should see a list of the news items, each of which has a link
to display just the one article.
Expand Down
80 changes: 41 additions & 39 deletions user_guide_src/source/tutorial/static_pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,51 @@ Static Pages
:doc:`installed the framework <../installation/index>` in your
development environment.

The first thing you're going to do is set up a **controller** to handle
static pages. A controller is simply a class that helps delegate work.
It is the glue of your web application.
The first thing you're going to do is set up routing rules to handle static pages.

Setting Routing Rules
*********************

Routing associates a URI with a controller's method. A controller is simply a
class that helps delegate work. We will create a controller later.

Let's set up routing rules. Open the routes file located at **app/Config/Routes.php**.

The only route directive there to start with should be:

.. literalinclude:: static_pages/003.php

This directive says that any incoming request without any content
specified should be handled by the ``index()`` method inside the ``Home`` controller.

Add the following lines, **after** the route directive for ``'/'``.

.. literalinclude:: static_pages/004.php
:lines: 2-

CodeIgniter reads its routing rules from top to bottom and routes the
request to the first matching rule. Each rule is a regular expression
(left-side) mapped to a controller and method name
(right-side). When a request comes in, CodeIgniter looks for the first
match, and calls the appropriate controller and method, possibly with
arguments.

More information about routing can be found in the :doc:`../incoming/routing`.

Here, the second rule in the ``$routes`` object matches a GET request
to the URI path **/pages**, and it maps to the ``index()`` method of the ``Pages`` class.

The third rule in the ``$routes`` object matches a GET request to a URI segment
using the placeholder ``(:segment)``, and passes the parameter to the
``view()`` method of the ``Pages`` class.

Let's Make our First Controller
*******************************

The next thing you're going to do is set up a **controller** to handle
static pages. A controller is simply a class that helps delegate work.
It is the glue of your web application.

Create Pages Controller
=======================

Expand Down Expand Up @@ -141,42 +179,6 @@ view.
throw errors on case-sensitive platforms. You can read more about it in
:doc:`../outgoing/views`.

Setting Routing Rules
*********************

We have made the controller. The next thing is to set routing rules.
Routing associates a URI with a controller's method.

Let's do that. Open the routes file located at **app/Config/Routes.php**.

The only route directive there to start with should be:

.. literalinclude:: static_pages/003.php

This directive says that any incoming request without any content
specified should be handled by the ``index()`` method inside the ``Home`` controller.

Add the following lines, **after** the route directive for ``'/'``.

.. literalinclude:: static_pages/004.php
:lines: 2-

CodeIgniter reads its routing rules from top to bottom and routes the
request to the first matching rule. Each rule is a regular expression
(left-side) mapped to a controller and method name
(right-side). When a request comes in, CodeIgniter looks for the first
match, and calls the appropriate controller and method, possibly with
arguments.

More information about routing can be found in the :doc:`../incoming/routing`.

Here, the second rule in the ``$routes`` object matches a GET request
to the URI path **/pages**, and it maps to the ``index()`` method of the ``Pages`` class.

The third rule in the ``$routes`` object matches a GET request to a URI segment
using the placeholder ``(:segment)``, and passes the parameter to the
``view()`` method of the ``Pages`` class.

Running the App
***************

Expand Down

0 comments on commit da6492d

Please sign in to comment.