From 334bf5079603ee9ceb06ade6130f8d2479a68a70 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 10 Oct 2023 07:59:53 +0900 Subject: [PATCH] docs: move Routing Settings up It is more intuitive to set up routing first, so that you can check your controller to work. --- .../source/tutorial/create_news_items.rst | 34 ++++---- .../source/tutorial/news_section.rst | 22 ++--- .../source/tutorial/static_pages.rst | 80 ++++++++++--------- 3 files changed, 69 insertions(+), 67 deletions(-) diff --git a/user_guide_src/source/tutorial/create_news_items.rst b/user_guide_src/source/tutorial/create_news_items.rst index 2388841b70b8..6443e674d450 100644 --- a/user_guide_src/source/tutorial/create_news_items.rst +++ b/user_guide_src/source/tutorial/create_news_items.rst @@ -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 ************* @@ -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 ****************** diff --git a/user_guide_src/source/tutorial/news_section.rst b/user_guide_src/source/tutorial/news_section.rst index 183b2f0c8570..935f79476054 100644 --- a/user_guide_src/source/tutorial/news_section.rst +++ b/user_guide_src/source/tutorial/news_section.rst @@ -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 ====================== @@ -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. diff --git a/user_guide_src/source/tutorial/static_pages.rst b/user_guide_src/source/tutorial/static_pages.rst index 9745cbb633de..db3382245195 100644 --- a/user_guide_src/source/tutorial/static_pages.rst +++ b/user_guide_src/source/tutorial/static_pages.rst @@ -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 ======================= @@ -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 ***************