Skip to content

Commit 031e4b8

Browse files
authored
Merge pull request #5285 from kenjis/update-tutorial
docs: update Tutorial
2 parents edacaa4 + 7f01e12 commit 031e4b8

12 files changed

Lines changed: 67 additions & 51 deletions

File tree

-7.69 KB
Loading
9.99 KB
Loading
32.5 KB
Loading
14 KB
Loading
8.21 KB
Loading
10.9 KB
Loading
65.5 KB
Loading

user_guide_src/source/installation/upgrade_configuration.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Upgrade Guide
2525
1. You have to change the values in the default CI4 config files according to the
2626
changes in the CI3 files. The config names are pretty much the same as in CI3.
2727
2. If you are using custom config files in your CI3 project you have to create those
28-
files as new php classes in your CI4 project in ``app/Config/``. These classes
28+
files as new PHP classes in your CI4 project in ``app/Config/``. These classes
2929
should be in the ``Config`` namespace and should extend ``CodeIgniter\Config\BaseConfig``.
3030
3. Once you have created all custom config classes, you have to copy the variables
3131
from the CI3 config into the new CI4 config class as public class properties.

user_guide_src/source/tutorial/create_news_items.rst

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ you haven't written any information to the database yet. In this section,
66
you'll expand your news controller and model created earlier to include
77
this functionality.
88

9+
Enable CSRF Filter
10+
------------------
11+
12+
Before creating a form, let's enable the CSRF protection.
13+
14+
Open the **app/Config/Filters.php** file and update the ``$methods`` property like the following::
15+
16+
public $methods = [
17+
'post' => ['csrf'],
18+
];
19+
20+
It configures the CSRF filter to be enabled for all **POST** requests.
21+
You can read more about the CSRF protection in :doc:`Security </libraries/security>` library.
22+
923
Create a form
1024
-------------
1125

@@ -19,7 +33,7 @@ the slug from our title in the model. Create a new view at
1933

2034
<h2><?= esc($title) ?></h2>
2135

22-
<?= \Config\Services::validation()->listErrors() ?>
36+
<?= service('validation')->listErrors() ?>
2337

2438
<form action="/news/create" method="post">
2539
<?= csrf_field() ?>
@@ -28,13 +42,13 @@ the slug from our title in the model. Create a new view at
2842
<input type="input" name="title" /><br />
2943

3044
<label for="body">Text</label>
31-
<textarea name="body"></textarea><br />
45+
<textarea name="body" cols="45" rows="4"></textarea><br />
3246

3347
<input type="submit" name="submit" value="Create news item" />
3448
</form>
3549

3650
There are probably only two things here that look unfamiliar. The
37-
``\Config\Services::validation()->listErrors()`` function is used to report
51+
``service('validation')->listErrors()`` function is used to report
3852
errors related to form validation. The ``csrf_field()`` function creates
3953
a hidden input with a CSRF token that helps protect against some common attacks.
4054

@@ -47,7 +61,7 @@ validation <../libraries/validation>` library to do this.
4761

4862
public function create()
4963
{
50-
$model = new NewsModel();
64+
$model = model(NewsModel::class);
5165

5266
if ($this->request->getMethod() === 'post' && $this->validate([
5367
'title' => 'required|min_length[3]|max_length[255]',
@@ -60,7 +74,6 @@ validation <../libraries/validation>` library to do this.
6074
]);
6175

6276
echo view('news/success');
63-
6477
} else {
6578
echo view('templates/header', ['title' => 'Create a news item']);
6679
echo view('news/create');
@@ -69,9 +82,9 @@ validation <../libraries/validation>` library to do this.
6982
}
7083

7184
The code above adds a lot of functionality. First we load the NewsModel.
72-
After that, we check if we deal with the ``POST`` request and then
85+
After that, we check if we deal with the **POST** request and then
7386
the Controller-provided helper function is used to validate
74-
the $_POST fields. In this case, the title and text fields are required.
87+
the user input data. In this case, the POST data, and the title and text fields are required.
7588

7689
CodeIgniter has a powerful validation library as demonstrated
7790
above. You can read :doc:`more about this library
@@ -83,7 +96,7 @@ was submitted **and** passed all the rules, the model is called. This
8396
takes care of passing the news item into the model.
8497
This contains a new function ``url_title()``. This function -
8598
provided by the :doc:`URL helper <../helpers/url_helper>` - strips down
86-
the string you pass it, replacing all spaces by dashes (-) and makes
99+
the string you pass it, replacing all spaces by dashes (``-``) and makes
87100
sure everything is in lowercase characters. This leaves you with a nice
88101
slug, perfect for creating URIs.
89102

@@ -106,9 +119,9 @@ or if the row already exists and should be updated, based on the presence
106119
of a primary key. In this case, there is no ``id`` field passed to it,
107120
so it will insert a new row into it's table, **news**.
108121

109-
However, by default the insert and update methods in the model will
122+
However, by default the insert and update methods in the Model will
110123
not actually save any data because it doesn't know what fields are
111-
safe to be updated. Edit the model to provide it a list of updatable
124+
safe to be updated. Edit the **NewsModel** to provide it a list of updatable
112125
fields in the ``$allowedFields`` property.
113126

114127
::
@@ -168,8 +181,8 @@ Congratulations
168181
You just completed your first CodeIgniter4 application!
169182

170183
The image underneath shows your project's **app** folder,
171-
with all of the files that you created in green.
172-
The two modified configuration files (Database & Routes) are not shown.
184+
with all of the files that you created in red.
185+
The two modified configuration files (**Config/Routes.php** & **Config/Filters.php**) are not shown.
173186

174187
.. image:: ../images/tutorial9.png
175188
:align: left

user_guide_src/source/tutorial/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ From your command line type the following:
6161

6262
composer create-project codeigniter4/appstarter ci-news
6363

64-
This creates a new folder, ci-news, which contains your application code, with
64+
This creates a new folder, **ci-news**, which contains your application code, with
6565
CodeIgniter installed in the vendor folder.
6666

6767
By default, CodeIgniter starts up in production mode. This is a safety feature
@@ -71,7 +71,7 @@ So first let's fix that. Copy or rename the ``env`` file to ``.env``. Open it up
7171
This file contains server-specific settings. This means you never will need to
7272
commit any sensitive information to your version control system. It includes
7373
some of the most common ones you want to enter already, though they are all commented
74-
out. So uncomment the line with CI_ENVIRONMENT on it, and change ``production`` to
74+
out. So uncomment the line with ``CI_ENVIRONMENT`` on it, and change ``production`` to
7575
``development``::
7676

7777
CI_ENVIRONMENT = development

0 commit comments

Comments
 (0)