Skip to content

Commit da6492d

Browse files
authored
Merge pull request #8022 from kenjis/docs-improve-tutorial
docs: improve tutorial
2 parents 155859b + 334bf50 commit da6492d

3 files changed

Lines changed: 69 additions & 67 deletions

File tree

user_guide_src/source/tutorial/create_news_items.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ You can read more about the CSRF protection in :doc:`Security <../libraries/secu
2626
because :ref:`auto-routing-legacy` permits any HTTP method to access a controller.
2727
Accessing the controller with a method you don't expect could bypass the filter.
2828

29+
Adding Routing Rules
30+
********************
31+
32+
Before you can start adding news items into your CodeIgniter application
33+
you have to add an extra rule to **app/Config/Routes.php** file. Make sure your
34+
file contains the following:
35+
36+
.. literalinclude:: create_news_items/004.php
37+
38+
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.
39+
40+
The ``$routes->post()`` line defines the router for a POST request. It matches
41+
only a POST request to the URI path **/news**, and it maps to the ``create()`` method of
42+
the ``News`` class.
43+
44+
You can read more about different routing types in :ref:`defined-route-routing`.
45+
2946
Create a Form
3047
*************
3148

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

154-
Adding Routing Rules
155-
********************
156-
157-
Before you can start adding news items into your CodeIgniter application
158-
you have to add an extra rule to **app/Config/Routes.php** file. Make sure your
159-
file contains the following:
160-
161-
.. literalinclude:: create_news_items/004.php
162-
163-
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.
164-
165-
The ``$routes->post()`` line defines the router for a POST request. It matches
166-
only a POST request to the URI path **/news**, and it maps to the ``create()`` method of
167-
the ``News`` class.
168-
169-
You can read more about different routing types in :ref:`defined-route-routing`.
170-
171171
Create a News Item
172172
******************
173173

user_guide_src/source/tutorial/news_section.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ that are going to display the news items to the user. This could be done
120120
in our ``Pages`` controller created earlier, but for the sake of clarity,
121121
a new ``News`` controller is defined.
122122

123+
Adding Routing Rules
124+
====================
125+
126+
Modify your **app/Config/Routes.php** file, so it looks as follows:
127+
128+
.. literalinclude:: news_section/008.php
129+
130+
This makes sure the requests reach the ``News`` controller instead of
131+
going directly to the ``Pages`` controller. The second ``$routes->get()`` line
132+
routes URI's with a slug to the ``show()`` method in the ``News`` controller.
133+
123134
Create News Controller
124135
======================
125136

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

201212
.. literalinclude:: news_section/007.php
202213

203-
Adding Routing Rules
204-
********************
205-
206-
Modify your **app/Config/Routes.php** file, so it looks as follows:
207-
208-
.. literalinclude:: news_section/008.php
209-
210-
This makes sure the requests reach the ``News`` controller instead of
211-
going directly to the ``Pages`` controller. The second ``$routes->get()`` line
212-
routes URI's with a slug to the ``show()`` method in the ``News`` controller.
213-
214214
Point your browser to your "news" page, i.e., **localhost:8080/news**,
215215
you should see a list of the news items, each of which has a link
216216
to display just the one article.

user_guide_src/source/tutorial/static_pages.rst

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,51 @@ Static Pages
99
:doc:`installed the framework <../installation/index>` in your
1010
development environment.
1111

12-
The first thing you're going to do is set up a **controller** to handle
13-
static pages. A controller is simply a class that helps delegate work.
14-
It is the glue of your web application.
12+
The first thing you're going to do is set up routing rules to handle static pages.
13+
14+
Setting Routing Rules
15+
*********************
16+
17+
Routing associates a URI with a controller's method. A controller is simply a
18+
class that helps delegate work. We will create a controller later.
19+
20+
Let's set up routing rules. Open the routes file located at **app/Config/Routes.php**.
21+
22+
The only route directive there to start with should be:
23+
24+
.. literalinclude:: static_pages/003.php
25+
26+
This directive says that any incoming request without any content
27+
specified should be handled by the ``index()`` method inside the ``Home`` controller.
28+
29+
Add the following lines, **after** the route directive for ``'/'``.
30+
31+
.. literalinclude:: static_pages/004.php
32+
:lines: 2-
33+
34+
CodeIgniter reads its routing rules from top to bottom and routes the
35+
request to the first matching rule. Each rule is a regular expression
36+
(left-side) mapped to a controller and method name
37+
(right-side). When a request comes in, CodeIgniter looks for the first
38+
match, and calls the appropriate controller and method, possibly with
39+
arguments.
40+
41+
More information about routing can be found in the :doc:`../incoming/routing`.
42+
43+
Here, the second rule in the ``$routes`` object matches a GET request
44+
to the URI path **/pages**, and it maps to the ``index()`` method of the ``Pages`` class.
45+
46+
The third rule in the ``$routes`` object matches a GET request to a URI segment
47+
using the placeholder ``(:segment)``, and passes the parameter to the
48+
``view()`` method of the ``Pages`` class.
1549

1650
Let's Make our First Controller
1751
*******************************
1852

53+
The next thing you're going to do is set up a **controller** to handle
54+
static pages. A controller is simply a class that helps delegate work.
55+
It is the glue of your web application.
56+
1957
Create Pages Controller
2058
=======================
2159

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

144-
Setting Routing Rules
145-
*********************
146-
147-
We have made the controller. The next thing is to set routing rules.
148-
Routing associates a URI with a controller's method.
149-
150-
Let's do that. Open the routes file located at **app/Config/Routes.php**.
151-
152-
The only route directive there to start with should be:
153-
154-
.. literalinclude:: static_pages/003.php
155-
156-
This directive says that any incoming request without any content
157-
specified should be handled by the ``index()`` method inside the ``Home`` controller.
158-
159-
Add the following lines, **after** the route directive for ``'/'``.
160-
161-
.. literalinclude:: static_pages/004.php
162-
:lines: 2-
163-
164-
CodeIgniter reads its routing rules from top to bottom and routes the
165-
request to the first matching rule. Each rule is a regular expression
166-
(left-side) mapped to a controller and method name
167-
(right-side). When a request comes in, CodeIgniter looks for the first
168-
match, and calls the appropriate controller and method, possibly with
169-
arguments.
170-
171-
More information about routing can be found in the :doc:`../incoming/routing`.
172-
173-
Here, the second rule in the ``$routes`` object matches a GET request
174-
to the URI path **/pages**, and it maps to the ``index()`` method of the ``Pages`` class.
175-
176-
The third rule in the ``$routes`` object matches a GET request to a URI segment
177-
using the placeholder ``(:segment)``, and passes the parameter to the
178-
``view()`` method of the ``Pages`` class.
179-
180182
Running the App
181183
***************
182184

0 commit comments

Comments
 (0)